Life Cycle of A Thread

When you are programming with threads, understanding the life cycle of thread is very valuable. While a thread is alive, it is in one of several states.

A thread can be in one of the five states in the thread. According to sun, there is only 4 states new, runnable, non-runnable and terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The thread states are as follows:
  1. New
  2. Runnable(Ready)
  3. Running
  4. Non-Runnable (Blocked, Sleeping, Waiting)
  5. Terminated(Dead)

Life Cycle of A Thread

States of thread- A thread has one of the following States.

New-A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

1-Runnable- After invocation of start() method on new thread, the thread becomes runnable.After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.
2-Running-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.
3-Blocked- This is the state when a thread is waiting for a lock to access an object.
4-Waiting-Sometimes a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

5-Timed waiting- A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.
6-Not Runnable- after Runnable states these three states are assumed to be in a not Runnable state. These three states are waiting, Timed_waiting(sleeping) and Terminated.
7-Terminated- In this state the thread is dead.

<<Previous <<   || Index ||   >>Next >>

No comments:

Post a Comment