Life Cycle of Thread

To analyse the Thread Dump it is necessary to know about the different states of the thread. These states are also called as “life cycle of the thread”. There are total 6 major states of the thread which are:

  1. New
  2. Runnable
  3. Blocked
  4. Waiting
  5. Timed Waiting
  6. Terminated

1. New: 

This is the initial state of the thread. When a thread is created, it is in the new state. The thread has not yet started to run when it is in this state.

2. Runnable State: 

A thread that is ready to run is moved from the New to Runnable state. In this state, a thread might actually be running or it might be in Ready To Run state. Once the thread comes in the Runnable state then it occupies the CPU and starts processing the code. In this state, there is an intermediate state called “Ready To Run”. Such a state comes because every thread runs for a short while and then pauses and relinquishes the CPU to another thread so that other threads can get a chance to run. But all such threads lay under Runnable State whether they are in Ready To Run or currently running state. It is the responsibility of the thread scheduler to give a chance to each thread to run.

3. Blocked: 

A thread is in a blocked state when it tries to access a protected section of code that is currently locked by some other thread. When the protected section is unlocked, the schedule picks the blocked thread and moves it to the Runnable state. In thread dump analysis, this state needs to keep in mind. We need to analyse which threads were in the blocked state and what was the reason.

4. Waiting: 

When a thread is in the waiting state, it means the thread is waiting for the response from another thread to fulfil some conditions. Once the condition is fulfilled the thread moves from Waiting to Runnable state. Similar to the Blocked state, the Waiting state also plays an important role during thread dump analysis. We analyse which threads were in the waiting state and what was the cause.

5. Timed Waiting: 

A thread lies in the state of timed waiting when it calls a method like wait(), sleep(), join(), and park() with a timeout parameter. A thread lies in this state until the timeout is completed or until a notification is received.

6. Terminated: 

When a thread terminates then it frees the occupancy of the CPU and no longer exists. A thread terminates when either the program has been executed completely or some unusual erroneous event, like a segmentation fault or an unhandled exception, occurred.

Additional Information:

There are two types of Thread.

  • Daemon Thread: Daemon thread is a low-priority thread and runs in the background to perform tasks such as garbage collection etc. A daemon thread has a number of non-daemon threads and it dies when all the associated non-daemon thread completed their execution or died. 
  • b. Non-Daemon Thread: It is a normal thread or also called “User Thread”.

You may be interested:

1 thought on “Life Cycle of Thread”

Leave a Comment