Thursday, January 5, 2012

THREAD STATES

A thread takes on various states from the time that it starts to the point when its run
method completes execution. You should be able to recognize the various thread states and
how a thread transitions from one state to another. This section discusses the details of
these various states.
The Thread class defines the following method for obtaining the current state of a
thread:
public Thread.State getState()
Thread.State is an enumeration defined in the Thread class that represents all the
possible states of threads. The Thread.State enumeration has the following values:
NEW The thread has been instantiated but not started yet.
RUNNABLE The thread is either currently running on the CPU or waiting to be scheduled by
the JVM for execution.
BLOCKED The thread is waiting for a monitor lock to become available. A thread becomes
blocked when attempting to enter a block of synchronized code.
WAITING The thread is waiting for another thread to perform a particular action. For
example, the thread might be waiting for another thread to call Object.notify on a specifi
c object or waiting for another thread to terminate due to a call to Thread.join .
TIMED_WAITING This state is similar to WAITING except the thread only waits until a specifi
ed time elapses. A thread enters this state with a call to Thread.join or Object.notify
with a timeout, or Thread.sleep .
TERMINATED The thread has run to completion. A terminated thread cannot be started
again.

No comments:

Post a Comment