Sometimes, a thread's hang is useful. For example, a separate thread can be used to display the day's time. If the user does not want to use the clock, the thread is suspended. In any case, suspending a thread is simple, and once it is suspended, restarting the thread is a simple matter.
Suspend, terminating and resuming threading mechanisms are different in Java 2 and earlier versions. Although you use Java 2 to write code, you still need to understand how these operations were done in the early Java environment. For example, you may need to update or maintain old code. You also need to understand why Java 2 has such a change. For these reasons, the following describes the original method of executing thread control, followed by the Java 2 method.
Suspend, resume, and terminate of a Java 1.1 or earlier thread
Prior to the JAVA2 version, the program pauses and restarts the thread using thread-defined suspend () and resume (). They are in the following form:
Final void suspend ()
final void resume ()
The following procedures describe 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 are the entry point for thread.
public void Run () {try {= i > 0; i--) {System.out.println (name + ":" + i);
Thread.Sleep (200);
The catch (Interruptedexception e) {System.out.println (name + "interrupted.");
} System.out.println (name + "exiting.");
} class Suspendresume {public static void main (String args[]) {newthread ob1 = new Newthread ("one");
Newthread ob2 = new Newthread ("two");
try {thread.sleep (1000);
Ob1.t.suspend ();
SYSTEM.OUT.PRINTLN ("Suspending thread One");
Thread.Sleep (1000);
Ob1.t.resume ();
SYSTEM.OUT.PRINTLN ("Resuming thread One"); Ob2.t.suspend();
SYSTEM.OUT.PRINTLN ("Suspending thread Two");
Thread.Sleep (1000);
Ob2.t.resume ();
SYSTEM.OUT.PRINTLN ("Resuming thread Two");
catch (Interruptedexception e) {System.out.println ("Main thread Interrupted");
Threads to finish try {System.out.println ("Waiting for threads to finish.")
Ob1.t.join ();
Ob2.t.join ();
catch (Interruptedexception e) {System.out.println ("Main thread Interrupted");
} System.out.println ("Main thread exiting.");
}
}
Part of the output of the program is as follows:
New Thread:thread[one,5,main]
one:15
new Thread:thread[two,5,main]
two:15
one:14
two:14 One:13
two:13
one:12
two:12
one:11 two:11 suspending thread one
two:10
Two: 9
two:8
two:7
two:6
resuming thread one
suspending thread two
one:10
one:9
One:8
One:7
one:6
resuming thread two
waiting for threads to finish.
Two:5
one:5
two:4
one:4
two:3
one:3
two:2 one:2 two:1 one:1 Two exiting.
One exiting.
Main thread exiting.
The thread class also defines stop () to terminate the thread. Its form is as follows:
Once the thread is terminated, it cannot be resumed by resume ().
suspend, recover, and terminate threads in Java
Thread-defined suspend (), resume (), and Stop () methods appear to be the perfect and convenient way to manage threads, and they cannot be used in new Java version programs. Here's why. The suspend () method of the thread class is not supported in JAVA2 because suspend () can sometimes cause serious system failures. Assuming that a thread of a critical data structure is locked, if the thread hangs there, these locked threads do not relinquish control of the resource. Other threads that are waiting for these resources may deadlock.
The Resume () method is also not approved. It does not cause problems, but it cannot be used independently from the Suspend () method. The Stop () method of the thread class is also objected to in Java 2. This is because the method can cause a serious system failure. Imagine that a thread is writing a precise and important data structure and completing only a fraction of it. If the thread terminates at this point, the data structure may remain in a crash state.
Because you can't use Suspend (), resume (), and Stop () methods to control threads in Java 2, you might think there's no way to stop, recover, and end threads. Fact Instead, the thread must be designed so that the run () method is periodically checked to determine if the thread should be suspended, to restore or terminate its own execution. Typically, this is done by creating a flag variable that indicates the state of the thread. As long as the flag is set to "running", the Run () method must continue to let the thread execute. If the flag is "suspend", the thread must be paused. If set to "Stop", the thread must terminate.
Of course, there are many ways to write such code, but the central theme should be the same for all programs.
The following example illustrates how the wait () and notify () methods inherited from object control the execution of threads. This example is similar to the previous program. However, the methods that were not endorsed were not used. Let's think about the execution of the program.
The Newtread class contains an instance variable SuspendFlag that is used to control the execution of a thread. It is initialized to false by the constructor. The Run () method contains a block that monitors the synchronization declarations of the SuspendFlag. If the variable is a true,wait () method is called to suspend the thread. The Mysuspend () method sets SuspendFlag to True. The Myresume () method sets SuspendFlag to false and invokes the Notify () method to evoke the thread. Finally, the main () method is modified to invoke the Mysuspend () and Myresume () methods.
Suspending and resuming a thread for JAVA2 class Newthread implements Runnable {String name;//Name of thread Th
Read T;
Boolean SuspendFlag;
Newthread (String threadname) {name = ThreadName;
t = new Thread (this, name);
System.out.println ("New thread:" + t);
SuspendFlag = false; T.start ();
Start the thread}//This are the entry point for thread.
public void Run () {try {= i > 0; i--) {System.out.println (name + ":" + i);
Thread.Sleep (200);
Synchronized (this) {while (SuspendFlag) {wait ();
catch (Interruptedexception e) {System.out.println (name + "interrupted.");
} System.out.println (name + "exiting.");
} void Mysuspend () {SuspendFlag = true;
} synchronized void Myresume () {SuspendFlag = false;
Notify (); } class Suspendresume {public static void main (String args[]) {newthread Ob1 = NEW Newthread ("one");
Newthread ob2 = new Newthread ("two");
try {thread.sleep (1000);
Ob1.mysuspend ();
SYSTEM.OUT.PRINTLN ("Suspending thread One");
Thread.Sleep (1000);
Ob1.myresume ();
SYSTEM.OUT.PRINTLN ("Resuming thread One");
Ob2.mysuspend ();
SYSTEM.OUT.PRINTLN ("Suspending thread Two");
Thread.Sleep (1000);
Ob2.myresume ();
SYSTEM.OUT.PRINTLN ("Resuming thread Two");
catch (Interruptedexception e) {System.out.println ("Main thread Interrupted");
Threads to finish try {System.out.println ("Waiting for threads to finish.")
Ob1.t.join ();
Ob2.t.join ();
catch (Interruptedexception e) {System.out.println ("Main thread Interrupted");
} System.out.println ("Main thread exiting.");
}
}
The output of the
program is the same as the previous program. In the back section of this book, you'll see more examples of controlling threads with the Java 2 mechanism. Although this mechanism is not as "clean" as the old method, it is a way to ensure that no errors occur at run time. It is the method that all new code must adopt.