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:
- Timed Waiting
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 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 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 chance to each thread to run.
A thread is in the 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 move it to 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?
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 fulfils then thread moves from Waiting to Runnable state. Similar to Blocked state, 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(), park() with a timeout parameter. A thread lies in this state until the timeout is completed or until a notification is received.
When a thread terminates then it frees the occupancy of CPU and no longer exists. A thread terminates when either the program has been executed completely or some unusual erroneous event, like segmentation fault or an unhandled exception occurred.
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. Daemon thread having 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”