|
|
Threads - Thread States
- each thread has a life-cycle all it's own
- during it's life-cycle it can exist in a number of states
- New
- Runnable
- Not Runnable
- Dead
| Note |
- These states are those used in the Sun Java Thread tutorial. Other references may use 'ready', 'waiting' or other terminology to describe the Runnable and Non-runnable states.
|
- New
- a new thread is an empty Thread object; no system resources have been allocated as yet. Calling any thread method other than start() causes an IllegalThreadStateException
- Runnable
- a thread enters the Runnable state after the start() method is invoked. The start() method allocates system resources, schedules the thread, and calls the threads's run() method. When the thread actually runs is determined by the scheduler
- Not Runnable
- a thread is not runnable when
- it's sleep() method is invoked
- it's wait() method is invoked
- it is blocked on I/O ie waiting on system resources to perform an input or output operation
the thread becomes runnable again when a specific condition has been met based on the action which put it in the not runnable state
- when the number of milliseconds called in sleep() have elapsed
- when the condition it is waiting on has changed and it receives a notify() or notifyAll() message
- when the required system resources are made available and the I/O completes
- Dead
- a thread enters the dead state when it's run() method completes.
an interrupt does not kill a thread
the destroy() method kills a thread dead but does not release any of it's object locks
Life Cycle of a Thread from Sun Thread Tutorial
- a thread can bounce between runnable and not runnable states as a result of either
- scheduling, or
- programmer control
|