same time, MainThread owns a and is waiting to get b. This program will never complete.
As this example illustrates, if your multithreaded program locks up occasionally, deadlock
is one of the first conditions that you should check for.
Suspending, Resuming, and Stopping Threads
Sometimes, suspending execution of a thread is useful. For example, a separate thread can
be used to display the time of day. If the user doesn't want a clock, then its thread can be
suspended. Whatever the case, suspending a thread is a simple matter. Once suspended,
restarting the thread is also a simple matter.
The mechanisms to suspend, stop, and resume threads differ between early versions of
Java, such as Java 1.0, and modern versions, beginning with Java 2. Although you should
use the modern approach for all new code, you still need to understand how these operations
were accomplished for earlier Java environments. For example, you may need to update or
maintain older, legacy code. You also need to understand why a change was made. For these
reasons, the next section describes the original way that the execution of a thread was controlled,
followed by a section that describes the modern approach.
Suspending, Resuming, and Stopping Threads Using Java 1.1 and Earlier
Prior to Java 2, a program used suspend( ) and resume( ), which are methods defined by
Thread, to pause and restart the execution of a thread. They have the form shown below:
final void suspend( )
final void resume( )
The following program demonstrates these methods:
// Using suspend() and resume().
class NewThread implements Runnable {
String name; // name of thread
Thread t;
NewThread(String threadname) {
name = threadname;
t = new Thread(this, name);
System.out.println("New thread: " + t);
t.start(); // Start the thread
// This is the entry point for thread.
public void run() {
try {
for(int i = 15; i > 0; i--) {
System.out.println(name + ": " + i);
} catch (InterruptedException e) {
System.out.println(name + " interrupted.");
System.out.println(name + " exiting.");
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks