Friday, 25 November 2011

Thread Lifecycle


During the life time of a thread, there are many states it can enter. They includes:

  1. Newborn state
  2. Runnable state
  3. Running state
  4. Blocked state
  5. Dead state

A thread is always in one of these five states. It can move from one state to another via a variety of ways as shown in Figure.



The different states of thread life cycle are explained as below.

Newborn state

When create a thread object, the thread is born and is said to be in Newborn state. The thread is not yet scheduled for running. At this state, do only one of the following things with it:

1.                 Schedule it for running using start( ) method.
2.                 Kill it using stop( ) method.

If scheduled, it moves to the runnable state. If  attempt to use any other method at this stage, an exception will be thrown.

Runnable state(start())

The runnable state means that the thread is ready for execution and is waiting for the availability of the processor. That is, the thread has joined the queue of threads that are waiting for execution. If all threads have equal priority, then they are given time slots for execution in round robin fashion, i.e., first-come,first-server manner. The thread that relinquishes control joins the queue at the end and again waits for its turn. This process of assigning time to thread is known as time-slicing.

However, if want a thread to relinquish control to another thread to equal priority before its turn comes,  do it by using the yield( ) method.

Running state

Running means that the processor has given its time to the thread for its execution. The thread runs until it relinquishes control on its own or it is preempted by a higher priority thread. A running thread may relinquish its control on one of the following situations.

1.     It has been suspended using suspend( ) method. A suspended thread can be revived by using the resume( ) method. This approach is useful when we want to suspend a thread for some time due to certain reason, but do not want to kill it.

2.     It has been made to sleep. We can put a thread to sleep for a specified time period using the method sleep( time) where time is in milliseconds. This means that the thread is out of the queue during this time period. The thread re-enters the runnable state as soon as this time period is elapsed.


3.     It has been told to wait until some event occurs. This is done using the wait( ) method. The thread can be scheduled to run again using the notify( ) method.

Blocked State

A thread  is said to be blocked when it is prevented from entering into the runnable state and subsequently the running state. This happens when the thread is suspended, sleeping, or waiting in order to satisfy certain requirements. A blocked thread is considered “not runnable” but not dead and therefore fully qualified to run again.

Dead State

Every thread has a life cycle. A running thread ends its life when it has completed executing its run( ) method. It is a natural death. However, we can kill it by sending the stop message to it at any state thus causing a premature death to it. A thread can be killed as soon it is born, or while it is running, or even when it is in “not runnable” (blocked) condition.

Posted By : Ruchita Pandya

No comments:

Post a Comment