Learn in Java
 


   

Thread States - Life cycle of a Thread

 
A Java thread can be in any of the following states :

Thread States:
 New
 Runnable
 Running
 Sleeping/Waiting/Blocked
 Stopped/Dead

Now, to understand these states in a better way, look at the below screenshot from a famous game, yes, Angry Birds :) Ofcourse, i modified the screenshot to make you understand the thread states in a simple and easy way.



Let us see each of the thread states in detail :

New : The thread object has been created but the thread is not considered alive as yet. Compare this to the birds in 'New' box, they are created but not in action i.e, alive in case of a thread.

Runnable : In this state, the thread is considered alive and ready to run. Compare this to the bird in the 'Runnable' box, the bird on the Y shaped stick is ready to hit the pig.

Running : The thread's run() method is called. The thread is alive and running. Compare this to the bird in the 'Running' box, the bird is in action.

Waiting/Sleeping/Blocked for IO : This state is where we see the threads awaiting for some response. A thread can be in waiting state when it requires a lock but is currently being used by some other thread. A thread can be in sleeping state when its sleep() method is called. A thread can be in blocked state when it is waiting for some IO operation to complete like reading/writing a file etc.

Compare this to the birds in the powerups bar. In the game, we have a few birds waiting in the powerup bar which can be made runnable at any moment. One difference in multithreading however is that the threads in these states becomes runnable only after they get what they wanted.

Stopped/Dead : In this state, the thread is considered dead and cannot be run. See the dead bird there ?

Rules to remember:
1. Starting a thread that is already running causes IllegalThreadStateException
2. Calling the run() method directly instead of start() method will not create a new thread instead only the method will be executed

 

Let us see the lifecycle of a thread. See the below screenshot. Notice the green arrows ? The green arrows shows the directions in which a thread state can move to/from another state.



As you can see a 'New' thread can only become 'Runnable' and a 'Runnable' thread can only become 'Running'. A 'Running' thread can become a Wating/Sleeping/Blocked thread. This is an exception to the angry birds rules. We know that a bird once running cannot move to powerup bar. Also, a 'Running' thread can become 'Runnable' and 'Stopped/Dead'. Finally, remember that any thread in the waiting/sleeping/blocked state can become a 'Runnable' thread only. They cannot be made 'Running' directly.

For the exact method call in each of the above cases, refer to the table below:

From State(s) To State(s) Method Name(s)
New Runnable  
Runnable Running run()
Running Waiting wait()/join()
Running Sleeping sleep()
Running Blocked  
Running Runnable yeild()
Waiting Runnable notify()/notifyAll()
Sleeping Runnable  
Blocked Runnable  
Running Stopped/Dead  


How to remember:
Simple! Remember the above two diagrams ;)
 
   

Related Articles

User and Daemon Threads
 
   

Recommended Articles

Java 8 Stream
Java 8 Functional Interfaces
Java 8 Lambda Expressions
Restful WebService using CXF
Restful Client using CXF
SOAP WebService using CXF




LIKE/SHARE
 
 
 

Comments:


 

Top Picks

1. Top 5 websites offering free e-books

2. Top 10 websites for free Java Articles and Tutorials

3. Top 5 websites for Java Certifications

4. Top 5 Softwares for Java developers




   
   
   
 
 
© Copyright 2017. All rights reserved. All trademarks and logos belongs to their owners.         Website Counter