What is the lifecycle of the Thread?
We have seen what is Multithreading in the Java, and how the thread is created and started. Once the execution of the thread is completed thread is ended. A lifecycle of a thread is basically a state of transition between the period when the thread is started and the thread is ended.
The thread has four main states New/Born, Runnable, Running, and Dead. Thread also has a waiting/ blocked state and a suspended state. Following is the diagram for the lifecycle of the thread.
Let’s check the lifecycle of Thread in Detail
- New / Born
When we create a new thread, it is always in the New state. For the thread in the new state, the start method is not called yet and the thread has not begun its execution. We can get the thread state by using the Thread.getState() method.
In the below example myThread is created but not started hence the state of MyThread is NEW.
public static void main(String args[]) {
MyThread myThread = new MyThread();
System.out.println(“State of MyThread is : “+ myThread.getState());
}
}
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(“Child Thread: ” + i);
}
}
}
- Ready / Runnable
When a thread invokes the start() method, it moves from the NEW state to the Ready/Runnable state. It means Thread is started and ready to run but not yet in a Running state. In the above example if we start the thread before printing the state. The Thread state will be the RUNNABLE state.
- Running
From Runnable state, Thread goes to RUNNING State. Runnable and Running Threads are active states of the thread and the Thread goes from Runnable to running state and from running to again runnable state until that thread is an active state.
- Terminated/Dead
Once the execution of the run method is completed the thread goes to the Terminated or Dead state. In the below example if the main thread is in a sleep state, by that time MyThread’s execution will be completed and the thread will go into the Terminated state.
public static void main(String args[]) throws InterruptedException {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(1000);
System.out.println(“State of MyThread is : ” + myThread.getState());
}
}
class MyThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(“Child Thread: ” + i);
}
}
}
Child Thread: 1
Child Thread: 2
Child Thread: 3
Child Thread: 4
State of MyThread is : TERMINATED
- Waiting / Blocked
Other than the above 4 states threads also have a Blocked / Waiting state. When a thread is inactive for some time then it’s in a Blocked or waiting state.
Let’s take the following example. If we use the sleep method on the thread for some time thread 1 will be in a waiting state.
public static void main(String args[]) throws InterruptedException {
MyThread myThread1 = new MyThread(“myThread1”);
myThread1.start();
Thread.sleep(500);
System.out.println(“State of MyThread 1 is : ” + myThread1.getState());
}
}
class MyThread extends Thread {
String threadName;
public MyThread(String name) {
threadName = name;
}
public void run() {
try {
this.sleep(5000);
} catch (InterruptedException e) {
}
System.out.print(threadName + “:”);
for (int i = 0; i < 5; i++) {
System.out.print(” ” + i);
}
System.out.println();
}
}
myThread1: 0 1 2 3 4
What are all states of the thread?
java.lang.Thread.State is an Enum in java which has Six States.
A thread that has not yet started is in this state.
- NEW: A thread that has not yet started is in this state.
- RUNNABLE: A thread executing in the JVM is in this state.
- BLOCKED: A thread that is blocked waiting for a monitor lock is in this state.
- WAITING: A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
- TIMED_WAITING: A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
- TERMINATED: A thread that has exited is in this state.