Life Cycle of Thread in Java

Thread states and Thread Life Cycle in Java is crucial when you are developing multithreaded application in Java. In this article, we will learn about the various states of thread in java and life cycle of thread in java.

Life Cycle of Thread (Thread states) in Java

Life cycle of thread in Java has five states. Thread in Java can be in one of the five states at any time instance. Thread can not exist in more than one state at one time. These five states are also called as Life cycle events of Thread in Java.

Thread states or Life Cycles of threads has following five states :

  • New
  • Runnable
  • Running
  • Non-Runnable (Blocked) / Waiting
  • Terminated / Dead

Let’s look at this below basic diagram :

Life Cycle of Thread in Java

New

As soon as a new thread is created, it enters in New state. That means, we have created the instance of Thread class but we have not called the start() method yet.

In this state, thread does not start to run as it is only created and we have not called the start() method yet. Thread is not considered as alive in the New state as it is not getting executed. Newย state is considered as internal state to Java programming.

When the start() method is called on the thread, the thread leaves the New state and it can now never return to the New state again in its lifetime.

class MyThread extends Thread {

public void run(){

........

}ย  }

public class MainApp{

public static void main(String args[]){

MyThread th1 = newย MyThread(); //Thread now enters in New state

System.out.prinltn("Thread current state : "+th1.getState());

}

}

Runnable

The thread is considered to be inย Runnable state,ย when the start() method is invoked in the thread but the thread scheduler has still not selected this thread for execution.

As we know, if we want to start the execution of any thread, we need to invoke the start() method but the execution will actually start when the thread scheduler selects the thread. That means, Thread scheduler has the responsibility to give the thread time to run.

Thread scheduling is platform dependent hence the algorithm used by thread scheduler in various platforms can vary. In most of the multi threaded application, each thread is allocated with a fixed amount of time for execution. Each thread runs for the fixed amount of time and then CPU is allocated to another thread and so on. In this process, all the threads which are ready to run but CPU is still not allocated then they are said to be in Runnable state.

class MyThread extends Thread { 

public void run(){ ........ }ย  

} 

public class MainApp{ 

public static void main(String args[]){ 

MyThread th1 = newย MyThread(); //Thread now enters in New state 

System.out.prinltn("Thread current state : "+th1.getState());

th1.start();ย //Thread now enters in Runnable state considering that 
             //thread-scheduler still not picked up the thread for execution

}ย  ย  }

Running

The thread is moved from Runnable state to Running state when thread scheduler selects it for execution and allocate the CPU to the thread. Thread can move to Runnable, Dead or Waiting state from the Running state.ย In the Running state of the thread, it starts executing its run() methods and it is considered to be alive.

Life Cycle of Thread in Java

class MyThread extends Thread { 

public void run(){ ........ }ย  

} 

public class MainApp{ 

public static void main(String args[]){ 

MyThread th1 = newย MyThread(); //Thread now enters in New state 

System.out.prinltn("Thread current state : "+th1.getState());

th1.start();ย //Thread enters in Running state when
             //thread-scheduler picks up the thread and allocate it CPU

}ย  ย  }

Non-Runnable (Blocked) / Waiting

A thread is considered to be in a Waiting state in below mentioned scenarios :

  • When a thread itself has called wait() method then the thread enters in Waiting state and it waits for the other thread to wake it up or notify it.
  • When sleep() method has been called on a thread, the thread enters in Waiting state.
  • When thread requires any I/O resources and is waiting it to be free.

In all of above scenarios, the thread is considered to be alive. As soon as thread leaves the Waiting state, it enters into Runnable state.

Terminated / Dead

A thread enters the Terminated or dead state when it successfully completesย its execution that meansย when its run() method exits or it is terminated due to error and got forcefully killed.

Below is the detailed diagram to conclude all the state of Threads:

Life Cycle of Thread in Java

Conclusion

Thatโ€™s all folks! In this article, you have got the complete overview of Life cycle of Thread in Java and States of Threads in Java.

Newsletter Updates

Enter your name and email address below to subscribe to our newsletter

Leave a Reply