In this tutorial, we'll delve into the lifecycle of a thread in Java. Threads are crucial in Java to execute multiple tasks simultaneously. Understanding their lifecycle is pivotal for efficient multithreading programming.
By the end of this tutorial, you will:
- Understand the different states of a thread.
- Learn how a thread moves from one state to another.
- Get hands-on experience with practical examples.
Prerequisites:
Basic knowledge of Java and object-oriented programming concepts.
In Java, a thread goes through various stages in its lifecycle. These stages include:
start()
method.start()
method, but the thread scheduler has not selected it to be the running thread.run()
method exits.start()
method.sleep()
, wait()
, etc.run()
method completes.Example 1: Thread creation and starting it
class MyThread extends Thread {
public void run() {
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String args[]) {
MyThread t1=new MyThread(); // Thread is in New state
t1.start(); // Thread moved to Runnable state
}
}
In the above example, t1
is a thread which is in new state initially. After calling the start()
method, it moves to the Runnable state and starts executing the run()
method.
Expected Output
Thread is running
Example 2: Moving thread to Non-Runnable state
class MyThread extends Thread {
public void run() {
try {
Thread.sleep(500); // Thread moved to Non-Runnable state
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread is running");
}
}
public class Main {
public static void main(String args[]) {
MyThread t1=new MyThread();
t1.start();
}
}
In this example, the thread t1
is put to sleep for 500 milliseconds, hence it moves to Non-Runnable state. After the sleep time is over, it moves back to Runnable state and prints "Thread is running".
Expected Output
Thread is running
In this tutorial, we've learned about the lifecycle of a thread in Java. We've looked at different states a thread can have and how a thread transitions from one state to another.
For further learning, you can explore more about thread synchronization and inter-thread communication in Java.
Exercise 1: Create a thread and move it to the Non-Runnable state by making it wait for a specific resource.
Exercise 2: Create multiple threads and observe their execution. Use methods like sleep()
and wait()
to move threads between different states.
Solutions:
Here are the solutions but try to solve them on your own before looking at these.
Exercise 1:
class MyThread extends Thread {
public void run() {
synchronized(this){
try {
wait(); // Thread will move to Non-Runnable state
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String args[]) {
MyThread t1=new MyThread();
t1.start();
}
}
Exercise 2:
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getId()
+" Value "+ i);
}
}
}
public class Main {
public static void main(String args[]) {
MyThread t1=new MyThread();
t1.start();
MyThread t2=new MyThread();
t2.start();
}
}
In this example, we are creating two threads. Both threads will execute simultaneously and print the values from 0 to 4 with a delay of 500 milliseconds between each print.