0% found this document useful (0 votes)
14 views

Multithreading in Java

multithreading

Uploaded by

TRANAND TR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Multithreading in Java

multithreading

Uploaded by

TRANAND TR
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MULTITHREADING IN JAVA

• Multithreading in Java allows concurrent execution of two or more parts of a program.


Each part is called a thread, and it is executed independently.
• Threads are light-weight processes within a process.
• Unlike in multiprocessing, threads use a shared memory area.
• Context-switching between the threads takes less time than process.
• Multithreading is particularly useful for tasks that are independent of each other and can
run simultaneously.
Examples:
✓ Handling multiple client requests in a server
✓ Performing background tasks in a GUI application

Life cycle of Thread

New: When a thread is created but not yet started.


Runnable: After the start() method is called, but it may not be running yet.
Running: Actively executing the run() method.
Blocked/Waiting: The thread is blocked or waiting for some condition (e.g., I/O or
synchronization).
Timed Waiting: A thread lies in a timed waiting state when it calls a method with a time-out
parameter. A thread lies in this state until the timeout is completed or until a notification is
received
Terminated: The thread has finished execution.

Thread Creation
Threads can be created by using two mechanisms:
1) Extending the Thread class
2) Implementing the Runnable Interface
By extending the Thread class
class MyThread extends Thread
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
System.out.println(i + " from " + Thread.currentThread().getName());
try
{
Thread.sleep(1000); // Pause the thread for 1 second
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
}
public class TestThread
{
public static void main(String[] args)
{
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
}
}

Output (Sample)
1 from Thread-0
1 from Thread-1
2 from Thread-0
2 from Thread-1
3 from Thread-0
3 from Thread-1
4 from Thread-0
4 from Thread-1
5 from Thread-0
5 from Thread-1

By implementing the Runnable interface


The Runnable interface has only one method: run(). This method must be implemented to
define the task that will be executed by a thread.

Syntax:
public interface Runnable
{
public void run();
}
// Class implementing the Runnable interface
class MyRunnable implements Runnable
{
public void run()
{
for (int i = 1; i <= 5; i++)
{
System.out.println(i + " from " + Thread.currentThread().getName());
try
{
Thread.sleep(1000); // Pause for 1 second
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
}

public class TestRunnable


{
public static void main(String[] args)
{
// Create an instance of the class that implements Runnable
MyRunnable myRunnable1 = new MyRunnable();
MyRunnable myRunnable2 = new MyRunnable();

// Create Thread objects, passing the Runnable objects to the Thread constructor
Thread t1 = new Thread(myRunnable1);
Thread t2 = new Thread(myRunnable2);
// Start the threads
t1.start();
t2.start();
}
}

You might also like