Tuesday, 17 November 2015

What are the different states of thread's lifecycle

New : When a thread is instantiated it is in New state until the start() method is called on the thread instance. In this state the thread is not considered to be alive.

Runnable :
The thread enters into this state after the start method is called in the thread instance. The thread may enter into the Runnable state from Running state. In this state the thread is considered to be alive.
Running : When the thread scheduler picks up the thread from the Runnable thread's pool, the thread starts running and the thread is said to be in Running state.
Waiting/Blocked/Sleeping : In these states the thread is said to be alive but not runnable. The thread switches to this state because of reasons like wait method called or sleep method has been called on the running thread or thread might be waiting for some i/o resource so blocked.
Dead : when the thread finishes its execution i.e. the run() method execution completes, it is said to be in dead state. A dead state cannot be started again. If a start() method is invoked on a dead thread a runtime exception will occur.
          

No comments:

Post a Comment