J2EEOnline J2EE   





Java2 Certification  «Prev  Next»
Lesson 4Thread States
Objective Describe the different processing states that apply to executing threads.
Thread states
Once a thread has been started, it may not execute immediately, and when it does, it rarely executes all the way through to completion. This is because the thread must share the system's CPU or CPUs with other threads.
A thread can be only in one of five states
  1. New: This is the state the thread is in after the Thread instance has been created but the start() method has not been invoked on the thread.
    It is a live Thread object, but not yet a thread of execution. At this point, the thread is considered not alive.
  2. Runnable: This is the state a thread is in when it is eligible to run but the scheduler has not selected it to be the running thread. A thread first enters the runnable state when the start() method is invoked, but a thread can also return to the runnable state after either running or coming back from a blocked, waiting, or sleeping state. When the thread is in the runnable state, it is considered alive.
  3. Running: This is where the action is. This is the state a thread is in when the thread scheduler selects it from the runnable pool to be the currently executing process. A thread can transition out of a running state for several reasons, including because "the thread scheduler felt like it." We will look at those other reasons shortly.
    There is only one way to get to the running state: The scheduler chooses a thread from the runnable pool.
  4. Waiting/blocked/sleeping: This is the state a thread is in when it's not eligible to run.
    This is really three states combined into one, but they all have one thing in common:
    The thread is still alive, but is currently not eligible to run. 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 waiting 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.
    For example, if data comes in through the input stream the thread code is reading from, or if the object's lock suddenly becomes available. A thread may be sleeping because the thread's run code tells it to sleep for some period of time, in which case, the event that sends it back to runnable causes it to wake up because its sleep time has expired
  5. Dead: A thread is considered dead when its run() method completes. It may still be a viable Thread object, but it is no longer a separate thread of execution. Once a thread is dead, it can never be brought back to life.
    If you invoke start() on a dead Thread instance, you will get a runtime (not compiler) exception. If a thread is dead, it is no longer considered alive.
The following table describes the circumstances under which a thread may move from one state to another:

From To Circumstance
New Runnable
(ready)
The thread is started as a separate execution sequence.
Runnable
Oracle Docs : Runnable
Running The thread is scheduled by the task scheduler.
Running Runnable
(ready)
  1. A higher priority thread comes into existence.
  2. The scheduler schedules a different thread.
  3. The thread invokes its yield() method.
Running Dead The thread's run() method completes its processing.
Running Waiting
  1. The thread invokes its sleep() method.
  2. The thread is blocked on I/O.
  3. The thread waits for a lock on an object or class.
  4. The thread invokes the wait() method of an object.
Waiting Runnable
(ready)
  1. A sleep() timeout has been expired. An I/O operation is completed.
  2. The thread acquires a lock for which it had been waiting.
  3. The thread is notified that an object for which it had been waiting has become available.
  4. A thread's interrupt() method is invoked.
In addition to the names listed above, there are other names for thread states.
Learning how a thread transitions from one state to another is key to mastering multithreading. The following MouseOver tooltip and the remainder of this module describe these state transitions.

Five Thread States