Java Thread Life Cycle – Java Thread States

Java Thread Life Cycle and Java Thread States are very important to any java developer who are working with Threads and programming for multithreaded environment.

Java Thread Life Cycle
Below diagram shows different states of thread life cycle in java. We can create a thread in java and start it but how the thread states change from Runnable to Running to Blocked depends on the OS implementation of thread scheduler and java doesn’t have full control on that.

There are six thread states. They are NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING and TERMINATED. A Thread will be in any one of these states at any point of time.

Let’s discuss these thread states one by one.

1.NEW
When we create a new Thread object using new operator, thread state is New Thread.

2.RUNNABLE
A thread will be in this state after calling the start() method. The control is given to Thread scheduler to finish it’s execution

3.BLOCKED
A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.

4.WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state. A thread will be in this state when wait() or join() method is called. Refer to the article Java Thread join.

5.TIMED_WAITING
A thread will be in this state when thread is sleeping. A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs. Refer to the article Java Thread sleep.

6.TERMINATED/DEAD
A thread will be in this state once it finishes it’s execution.
Lets run this program and see the output.

Output
Java Thread Life Cycle

References
Java Thread Life Cycle

Download the complete source code, click link below

JavaMultithreadExample-1.zip (1 download)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons