Concurrent programming study notes (Third, how to safely terminate the thread)

table of Contents:

  • Set exit identifier
  • interrupt () method
  • to sum up

Set exit identifier:

 1 public class FlagThread extends Thread {
 2     /**
 3      * 退出标识
 4      */
 5     public volatile boolean exit = false;
 6 
 7     @Override
 8     public void run() {
 9         while (!exit) {
10         }
11         System.out.println("ThreadFlag线程退出");
12     }
13 
14     public static void main(String[] args) throws{Exception
 15          FlagThread threadFlag = new new FlagThread ();
 16          threadFlag.start ();
 . 17          // main thread (3 seconds) 
18 is          SLEEP (3000 );
 . 19          // TODO thread terminates Thread 
20 is          threadFlag.exit = to true ;
 21 is          // main cpu thread to give up the right to use
 22          // make threadFlag thread to continue until threadFlag run finished 
23          threadFlag.join ();
 24-          System.out.println ( "thread exits!" );
 25      }
 26 }

By exit property on line 5 to indicate whether the thread should withdraw; but this method has a drawback, if the thread is blocked identifies it does not work.

interrupt () method:

 1 public class InterruptThread extends Thread {
 2     /**
 3      * 退出标识
 4      */
 5     volatile boolean exit = false;
 6 
 7     @Override
 8     public void run() {
 9         while (!exit) {
10             System.out.println(getName() + " is running");
11             try {
12                 Thread.currentThread().join();
13             } catch(InterruptedException E) {
 14                  System.out.println ( "up from Week Block ..." );
 15                  // modify shared variables exception handling code state 
16                  Exit = to true ;
 . 17              }
 18 is          }
 . 19          the System.out. the println (getName () + "exiting The IS ..." );
 20 is      }
 21 is  
22 is      public  static  void main (String [] args) throws InterruptedException {
 23 is          InterruptThread InterruptThread = new new InterruptThread ();
 24         System.out.println ( "Starting Thread ..." );
 25          interruptThread.start ();
 26 is          the Thread.sleep (3000 );
 27          System.out.println ( "Interrupt Thread ...:" + interruptThread.getName ( ));
 28          // set identifier to exit to true 
29          interruptThread.exit = to true ;
 30          interruptThread.interrupt ();
 31 is          // main thread sleeps 3 seconds in order to observe the thread interruption interruptThread 
32          the Thread.sleep (3000 );
 33 is          the System .out.println ( "Stopping file application ..." );
 34 is      }
 35 }

If line 30 notes, then run to the line 12 to hand over the right to use sub-thread CPU InterruptThread after the main thread is finished, then the child thread will be blocked; 29 led to the identification of the row useless.

Among them is the case only need to take the initiative to break the thread (30 lines of code).

 

 

 

 

 

 

 

 

 

 

public class InterruptThread extends Thread {
/**
* 退出标识
*/
volatile boolean exit = false;

@Override
public void run() {
while (!exit) {
System.out.println(getName() + " is running");
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
System.out.println("week up from block...");
// modify shared variables exception handling code state
Exit = to true;
}
}
the System. OUT .println (getName () + "exiting The IS ..." ) ;
}

public static void main (String [] args) throws {InterruptedException
InterruptThread InterruptThread = new new InterruptThread () ;
. the System OUT .println ( "Starting Thread ..." ) ;
interruptThread.start () ;
. the Thread SLEEP ( 3000 ) ;
. the System OUT .println ("Interrupt Thread ...:" + interruptThread.getName ()) ;
// set identifier to exit to true
InterruptThread. Exit = to true;
interruptThread.interrupt () ;
// Sleep main thread 3 seconds in order to observe the thread InterruptThread outages
Thread . SLEEP ( 3000 ) ;
. the System OUT .println ( "Stopping file application ..." ) ;
}
}

Guess you like

Origin www.cnblogs.com/bzfsdr/p/11565280.html