Wednesday, 1 February 2012

Life Cycle of a Thread in Java

The following diagram shows the life cycle of a Java Thread. Thread has following states
  • New
  • Runnable
  • Running
  • Sleeping/Blocked/Waiting
  • Dead
These states can be changed to each other as shown in the diagram(NOTE:Please click on the diagram to enlarge it).


Let us discuss the above thread states one by one.

New state

After the creations of Thread instance the thread is in this state but before the start() method invocation. At this point, the thread is considered not alive.At this stage no resource is allocated for the thread.


       class MyThread extends Thread {

        public void run() {
            System.out.println("MyThread running");
        }
    }


MyThread thread1=new MyThread();



where thread1 is in New state. 

Runnable (Ready-to-run) state 

A thread start its life from Runnable state. A thread first enters runnable state after the invoking of start() method but a thread can return to this state after either running, waiting, sleeping or coming back from blocked state also. On this state a thread is waiting for a turn on processor. 

thread1.start(); 


where thread1 is in Runnable state.At this stage the thread is eligible to run, but the scheduler has not selected it to be the running thread.When the thread is in the runnable state,it is considered alive.

Running state 

A thread is in running state that means the thread is currently executing. There are several ways to enter in Runnable state but there is only one way to enter in Running state: the scheduler select a thread from runnable pool.When the scheduler selects a thread to run,the run() method will be invoked by the JVM.


Waiting/blocked/sleeping state

At this stage the thread is not eligible to run but the thread is still alive.In other words, it is not runnable, but it might return to a runnable state later if a particular event occurs. 


A thread may be blocked for a resource (like I/O or an object's lock ),in which case the event that sends it back to runnable is the availability of the resource.

The thread may be blocked, because the thread's run code( t.join() ) causes it to wait, in which case the event that sends it back to runnable is that another thread t finishes its execution.


A thread may be sleeping because the thread's run code( Thread.sleep() ) tells it to sleep for some period of time,in which case the event that sends it back to runnable is that it wakes up because its sleep time has expired.If some other thread interrupts the sleeping thread ,then also it will move to runnable state.

The thread may be waiting, because the thread's run code( wait() ) causes it to wait, in which case the event that sends it back to runnable is that another thread sends a notification(notify(), notifyAll() ) that it may no longer be necessary for the thread to wait.

The important point is that one thread does not tell another thread to block. Some methods may look like they tell another thread to block, but they don't. If you have a reference t to another thread, you can write something like this:t.sleep() or t.yield()  .But those are actually static methods of the Thread class—they don't affect the instance t; instead they are defined to always affect the thread that's currently executing. 


Dead state 

A thread can be considered dead when its run() method completes. If any thread comes on this state that means it cannot ever run again.If you invoke start() on a dead Thread instance, you'll get a runtime exception-java.lang.IllegalThreadStateException.

No comments:

Post a Comment