Home>

Sometimes the hang of a thread is useful.For example, a separate thread can be used to display the time of day.If the user does not want to use the clock,The thread is suspended.In any case,Suspending a thread is very simple,Once suspended,Restarting the thread is also a simple matter.

The suspend, terminate, and resume thread mechanisms are different in java 2 and earlier versions.Although you write code using the Java 2 approach,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 changed this way.For these reasons,The following describes the original method of performing thread control,Followed by java 2 method.

Suspend, resume, and terminate threads for java 1.1 or earlier

Prior to the Java2 version, the program used suspend () and resume () defined by thread to pause and restart the thread.They have the 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 is the entry point for thread.
  public void run () {
    try {
      for (int i=15;i>0;i--) {
        system.out.println (name + ":" + i);
        thread.sleep (200);
      }
    } 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");
    }
    //wait for 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 program output 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.It has the form:

 void stop ()

Once the thread is terminated,It cannot be resumed by resume () to continue running.

Suspend, resume and terminate threads in java

The suspend (), resume () and stop () methods defined by thread seem to be the perfect and convenient way to manage threadsThey cannot be used in new java versions of programs.Here's why.The suspend () method of the thread class is deprecated in Java 2.Because suspend () sometimes causes serious system failure.Suppose a thread is locked on a critical data structure,If the thread hangs there,These locked threads did not relinquish control of the resource.Other threads waiting for these resources may deadlock.

The resume () method is also disapproved.It does not cause problems,But you cannot leave the suspend () method and use it independently.The stop () method of the thread class is also deprecated in Java 2.This is because this method can cause severe system failure.Imagine that a thread is writing a sophisticated important data structure and is completing only a fraction.If the thread terminates at this point,The data structure may stay in a crashed state.

Because suspend (), resume () and stop () methods cannot be used to control threads in Java 2,You might think that there is no way to stop,Resume and end threads.actually not.Instead, the thread must be designed so that the run () method periodically checks to determine if the thread should be suspended,Resume or terminate its own execution.Representative,This is done by creating a flag variable indicating the status of the thread.As long as this flag is set to "running", the run () method must continue to let the thread execute.If the flag is "suspend", the thread must be suspended.If set to "stop", the thread must be terminated.

Of course, there are many ways to write such code,But the central theme should be the same for all procedures.

The following example illustrates how the wait () and notify () methods inherited from object control thread execution.This example is very similar to the procedure described earlier.However, none of the approved methods were used.Let's think about the execution of the program.

The newtread class contains a boolean instance variable suspendflag that controls the execution of the thread. It is initialized to false by the constructor. The run () method contains a block that monitors the synchronous declaration of suspendflag.If the variable is true, the wait () method is called to suspend the thread.The mysuspend () method sets suspendflag to true. The myresume () method sets suspendflag to false and calls the notify () method to awaken the thread.Finally, the main () method is modified to call mysuspend () and myresume () methods.

//suspending and resuming a thread for java2
class newthread implements runnable {
  string name;//name of thread
  thread 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 is the entry point for thread.
  public void run () {
    try {
      for (int i=15;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");
    }
    //wait for 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 this program is the same as the previous program.Later in this book,You will see more examples of controlling threads with 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 runtime.It is the method that all new code must take.

  • Previous Collect and sort 17 common regular expressions in ASPNET
  • Next Java implementation of input and output stream code sharing