Friday, May 29, 2009

Different states of a thread

The job of the thread scheduler is to move a thread from one state to another state. A thread can be in one of the following states:

  1. New: It is the state when the thread instance has been created, but the start method has not been invoked on the thread.


  2. Runnable: It is the state when the thread is eligible to run. In this state, the thread is not selected by the scheduler for execution. A thread enters the Runnable state when the start() method is invoked. A thread can return to the Runnable state from the blocked, waiting, or sleeping state.


  3. Running: It is the state when the thread scheduler selects the thread to be the currently executing process. A thread can transition out of a running state through the compiler.


  4. Waiting: It is also known as blocked or sleeping state. In this state, the thread is not eligible to run, but it might return to the runnable state if a particular event occurs. A thread can be blocked if it is waiting for a resource. It can be in the waiting state if its code causes it to wait.


  5. Dead: A thread is considered dead when its run() method completes. Once a thread is dead, it can never be brought back to life.

No comments:

Post a Comment