Rajinder Yadav - C++ Windows Development Tools & Resources :Design, Code, Test, Debug

Thread Concepts, by Rajinder Yadav
March 5, 2002

Thread States

We talked about some states already, but Java define seven states a thread can be in, they are: created, ready, running, waiting, blocked, sleeping and dead. Here is a state transition diagram to explain the life-cycle of a thread from creation to termination.

   +-------+             +---------+
   |created|-->(start)-->|  ready  |<-----<-----<-----+<--------+
   +-------+             +---------+                  |         |
   (thread)                |     ^                    |         |
                           v     |                    ^         |
                         +---------+                  |         ^
                         |scheduler|                  |         |
                         +---------+                  ^         |
                   CPU time|     ^ CPU time           |         |
                   granted |     | spent          +--------+    ^
                           |     |            +-->|sleeping|    |
                           v     |            |   +--------+    |
      +----+              +-------+           |                 |
      |dead|<----(exit)---|running|-->(sleep)-+  +-->(notify)-->+
      +----+    thread    +-------+              |              |
                finishes      |||           +-------+           |
                              ||+-->(wait)->|waiting|           |
                              ||            +-------+           |
 +-------+                    |+--->(yield)--->----->---->------+
 |       |<--(operation call)-+                                 ^
 |blocked|                                                      |
 |       |-->(call returns)----->-------->-------->-------------+ 
 +-------+

Created, when a thread is create it is not given any systems resources or CPU time.

Ready, when a thread is moved into this state, it's given its own stack frame and waits to be given CPU time.

Running, in this state the thread has access to system resource it has locked (attained) and is given CPU cycles to carry out it's task.

Waiting, in this state the thread releases it's lock on resources and gives up it CPU cycles. The thread waits to be notified when to run again. When a notification arrives, the thread is moved into the ready state.

Blocked, in this state the thread retains it's lock on acquired resources and is, i) waiting for a system call to return, or ii) waiting for some resource to become available.

Sleeping, in this state the thread is temporarily 'suspended' for a specified amount of time, it's has voluntarily given up it's CPU cycles, but the thread keeps a hold on all locked resources. When the 'sleep' time has elapsed, the thread is 'woken' up and moved into the ready state.

Dead, a thread in this state had either completed or been terminated. A dead thread cannot be re-run again and all locked resources are released back to the system.

Copyright © 2002 Rajinder Yadav, All Rights Reserved

Home