Thread States - Life cycle of a Thread

Thread States - Life cycle of a Thread

learninjava
Mar 24, 2015 - Java
 

Thread States 

A Java thread can be in any of the following states :
 Remember!
  • 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.
    https://github.com/learninjavagithub/assets/raw/master/articles/thread-states.jpg
    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 ?
     Remember!
    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.
    https://github.com/learninjavagithub/assets/raw/master/articles/thread-states-lifecycle.jpg
    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)
    NewRunnable
    RunnableRunningrun()
    RunningWaitingwait()/join()
    RunningSleepingsleep()
    RunningBlocked
    RunningRunnableyeild()
    WaitingRunnablenotify()/notifyAll()
    SleepingRunnable
    BlockedRunnable
    RunningStopped/Dead
     Remember!
    How to remember:
    Simple! Remember the above two diagrams ;)
    Thats all folks !! Happy coding. If you feel this helped you, keep supporting us by   or  or  below or on the articles on social media.
     
    Like us on: