Java Reference
In-Depth Information
Instead of using these methods, you should have some variable that can be accessed by
both the thread whose state you wish to change, and by the thread that wishes to change the
state. The thread whose state is being changed should monitor this variable periodically, and
safely change its state if the variable changes (by releasing any resources, closing files, etc.).
For more information, read the description of why these methods have been deprecated
at http://java.sun.com/j2se/1.5.0/docs/guide/misc/threadPrimitiveDeprecation.html .
Thread States
Threads have six states: Thread.State.NEW , Thread.State.RUNNABLE , Thread.State.WAITING ,
Thread.State.TIMED_WAITING , Thread.State.BLOCKED , or Thread.State.TERMINATED . Java has
specific rules about the transitions from state to state, and it's important to know them.
The most important rule is that a Thread.State.TERMINATED thread cannot go into any
other state. Ever. The next most important rule is that a thread can only execute commands
if it is Thread.State.RUNNABLE . When you first call the start method on a thread, the thread
scheduler will transition it to Thread.State.RUNNABLE . At some point after that, depending on
the whims of the thread scheduler, the thread will start to execute the statements in the run
method body.
From the Thread.State.RUNNABLE state, a thread has four paths it can take, as illustrated in
Figure 4-11:
It can go on to completion, after which it enters the Thread.State.TERMINATED state.
A thread can enter the Thread.State.WAITING state by calling wait or join .
If the thread calls sleep or wait with a timeout value, it will enter the
Thread.State.TIMED_WAITING state.
If a thread cannot access a lock, it enters the Thread.State.BLOCKED state.
Figure 4-11. Thread states
Search WWH ::




Custom Search