Sunday 23 November 2014

Life cycle of a thread in java

Life cycle of a thread in java
Life cycle of a thread contains following stages :
New
Runnable
Running
Blocked or Non running
Terminated

 Release 5.0 introduced the Thread.getState method. When called on a thread, one of the following Thread.State values is returned:
  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • TIMED_WAITING
  • TERMINATED

New : State of Thread before start() method .

Runnable : A thread is in running state after invocation of start() method but has not yet selected by scheduler .

Running : After Scheduler select the thread to run and run() method starts executing then that thread it comes in running state.

Blocked or NonRunning : when a thread goes into wait() or sleep state then its called to be in non running or blocked .It comes into runnable state only if notify or notifyAll() is called on that thread.

Terminated : after a thread is being stopped by stop () method ,it comes into terminated state and can never go back to runnable or running state .



No comments:

Post a Comment