Thread can have different states.
■ 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.
■ Runnable This is the state a thread is in when it's 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.
■ Running This is it. The "big time." 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'll look at those other reasons shortly. Note that in Figure 9-2, there are
several ways to get to the runnable state, but only one way to get to the running
state: the scheduler chooses a thread from the runnable pool.
■ Waiting/blocked/sleeping This is the state a thread is in when it's not
eligible to run. Okay, so 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.
■ 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! (The
whole "I see dead threads" thing.) If you invoke start() on a dead Thread
instance, you'll get a runtime (not compiler) exception. And it probably
doesn't take a rocket scientist to tell you that if a thread is dead, it is no
longer considered to be alive.
No comments:
Post a Comment