Understanding the different thread states is important for analyzing the thread dump. The “thread lifecycle” is another name for these stages. The thread is in 6 main states in total, which are as follows:
- New
- Executable
- Blocked
- Waiting
- Time Wait
- Terminated
- New:
This is the initial state of the thread. A thread is in the new state when it is created. When in this state, the thread has not yet started executing. - Executable state
The state of a thread changes from New to Runnable when it is ready to run. A thread can be ready to run or it can be running in this state. The thread takes up CPU time and starts processing the code as soon as it enters the Runnable phase. There is a transition phase in this state known as “Ready To Run”. Such a situation results from each thread running for a short amount of time before pausing and handing over the CPU to another thread so that other threads can have a chance. carry out. But regardless of whether they are in the Ready to Run or Running state, all of these threads are in the Runnable State category. The thread scheduler is responsible for giving each thread a chance to run. - Blocked
When a thread tries to access a protected area of code that is currently locked by another thread, it enters the locked state. The program selects the blocked thread and moves it to the Runnable state when the protected section is unlocked. This state must be taken into account when running a thread dump analysis. We need to investigate which thread is in a blocked state and why? - Waiting
When a thread is waiting, it means that it is waiting for the response of another thread to fulfill specific requirements. The thread changes state from Waiting to Runnable when the condition is met. As with the blocked state, the wait state is crucial for examining thread dumps. We are investigating the causes of the threads being on hold. - Timed Wait:Β
When a thread calls a method such as wait(), sleep(), join(), or park() with a timeout parameter, A thread remains in this state until the timeout expires or a notification is received. - Terminated:Β
When a thread terminates, it releases the occupancy of the CPU. A thread ends when the program ends or when an exceptional error occurs, such as a segmentation fault or unhandled exception.
Additional Details:
There are two different types of thread.
- Daemon thread β The daemon thread runs in the background and Garbage collection is also an important task. A daemon thread can be associated with a number of non-daemon threads and terminates when all those threads finish executing or die.
- Non-Daemon Thread: It is a normal thread or also called βUser Threadβ.