J2EEOnline J2EE






Threading Model   «Prev 

Five Thread States

Five Thread States
Five Thread States
New to Runnable A thread object's start method is invoked causing the thread's run() method to be invoked by the JVM.
Runnable to Running The task scheduler schedules the thread based on the thread's priority and its scheduling algorithm.
Running to Runnable
A thread invokes its yield() method; or the task scheduler schedules another thread.
Running to Waiting
A thread invokes its sleep() method; or a thread blocks on I/O; or a thread waits for a lock; or a thread invokes an object's wait() method.
Waiting to Runnable A thread has finished sleeping; or an I/O operation is completed; or a thread acquires a lock; or a thread is notified that an object is available; or a thread's interrupt() method is invoked.
Running to Dead A thread's run() method returns.


Deprecated Methods of Thread

In addition to the useful methods of Thread, there are a number of unsafe methods that the developer should not use. These methods form part of the original Java thread API, but were quickly found to be not suitable for developer use. Unfortunately, due to Java's backward compatibility requirements, it has not been possible to remove them from the API. The developer simply needs to be aware of them, and to avoid using them under all circumstances.

stop()

Thread.stop() is almost impossible to use correctly without violating concurrent safety, as stop() kills the thread immediately, without giving it any opportunity to recover objects to legal states. This is in direct opposition to principles such as concurrent safety, and so should never be used.

suspend(), resume(), and countStackFrames()

The suspend() mechanism does not release any monitors it holds when it suspends, so any other thread that attempts to accesses those monitors will deadlock. In practice, this mechanism produces race conditions between these deadlocks and resume(), that render this group of methods unusable.

destroy()

This method was never implemented and it would have suffered from the same race condition issues as suspend() if it had been. All of these deprecated methods should always be avoided. Instead, a set of safe alternative patterns that achieve the same intended aims as the preceding methods have been developed. A good example of one of these patterns is the run-until shutdown pattern.