如何中断线程

来源:互联网 发布:oracle数据库去重复 编辑:程序博客网 时间:2024/05/01 22:32

线程的thread.interrupt()方法是中断线程,将会设置该线程的中断状态位,即设置为true,中断的结果线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序本身。线程会不时地检测这个中断标示位,以判断线程是否应该被中断(中断标示值是否为true)。它并不像stop方法那样会中断一个正在运行的线程。

判断线程是否被中断

判断某个线程是否已被发送过中断请求,请使用Thread.currentThread().isInterrupted()方法(因为它将线程中断标示位设置为true后,不会立刻清除中断标示位,即不会将中断标设置为false),而不要使用thread.interrupted()(该方法调用后会将中断标示位清除,即重新设置为false)方法来判断,下面是线程在循环中时的中断方式:

Java代码

  1. while(!Thread.currentThread().isInterrupted() && more work to do){   
  2.     do more work   
  3.  

while(!Thread.currentThread().isInterrupted() && more work to do){

   do more work

}

 如何中断线程

如果一个线程处于了阻塞状态(如线程调用了thread.sleepthread.jointhread.wait1.5中的condition.await、以及可中断的通道上的 I/O 操作方法后可进入阻塞状态),则在线程在检查中断标示时如果发现中断标示为true,则会在这些阻塞方法(sleepjoinwait1.5中的condition.await及可中断的通道上的 I/O 操作方法)调用处抛出InterruptedException异常,并且在抛出异常后立即将线程的中断标示位清除,即重新设置为false。抛出异常是为了线程从阻塞状态醒过来,并在结束线程前让程序员有足够的时间来处理中断请求。

 

注,synchronized在获锁的过程中是不能被中断的,意思是说如果产生了死锁,则不可能被中断(请参考后面的测试例子)。与synchronized功能相似的reentrantLock.lock()方法也是一样,它也不可中断的,即如果发生死锁,那么reentrantLock.lock()方法无法终止,如果调用时被阻塞,则它一直阻塞到它获取到锁为止。但是如果调用带超时的tryLock方法reentrantLock.tryLock(long timeout, TimeUnit unit),那么如果线程在等待时被中断,将抛出一个InterruptedException异常,这是一个非常有用的特性,因为它允许程序打破死锁。你也可以调用reentrantLock.lockInterruptibly()方法,它就相当于一个超时设为无限的tryLock方法。

 

没有任何语言方面的需求一个被中断的线程应该终止。中断一个线程只是为了引起该线程的注意,被中断线程可以决定如何应对中断。某些线程非常重要,以至于它们应该不理会中断,而是在处理完抛出的异常之后继续执行,但是更普遍的情况是,一个线程将把中断看作一个终止请求,这种线程的run方法遵循如下形式:

Java代码

  1. public void run() {   
  2.     try {   
  3.         ...   
  4.         /*  
  5.          * 不管循环里是否调用过线程阻塞的方法如sleepjoinwait,这里还是需要加上  
  6.          * !Thread.currentThread().isInterrupted()条件,虽然抛出异常后退出了循环,显  
  7.          * 得用阻塞的情况下是多余的,但如果调用了阻塞方法但没有阻塞时,这样会更安全、更及时。  
  8.          */  
  9.         while (!Thread.currentThread().isInterrupted()&& more work to do) {   
  10.             do more work    
  11.         }   
  12.     } catch (InterruptedException e) {   
  13.         //线程在waitsleep期间被中断了   
  14.     } finally {   
  15.         //线程结束前做一些清理工作   
  16.     }   
  17. }  

public void run() {

 try {

   ...

   /*

    * 不管循环里是否调用过线程阻塞的方法如sleepjoinwait,这里还是需要加上

    * !Thread.currentThread().isInterrupted()条件,虽然抛出异常后退出了循环,显

    * 得用阻塞的情况下是多余的,但如果调用了阻塞方法但没有阻塞时,这样会更安全、更及时。

    */

   while (!Thread.currentThread().isInterrupted()&& more work to do) {

     do more work

   }

 } catch (InterruptedException e) {

   //线程在waitsleep期间被中断了

 } finally {

   //线程结束前做一些清理工作

 }

}

上面是while循环在try块里,如果trywhile循环里时,因该在catch块里重新设置一下中断标示,因为抛出InterruptedException异常后,中断标示位会自动清除,此时应该这样:

Java代码

  1. public void run() {   
  2.     while (!Thread.currentThread().isInterrupted()&& more work to do) {   
  3.         try {   
  4.             ...   
  5.             sleep(delay);   
  6.         } catch (InterruptedException e) {   
  7.             Thread.currentThread().interrupt();//重新设置中断标示   
  8.         }   
  9.     }   
  10. }  

public void run() {

while (!Thread.currentThread().isInterrupted()&& more work to do) {

 try {

  ...

  sleep(delay);

 } catch (InterruptedException e) {

  Thread.currentThread().interrupt();//重新设置中断标示

 }

}

}

 

底层中断异常处理方式

另外不要在你的底层代码里捕获InterruptedException异常后不处理,会处理不当,如下:

 

Java代码

  1. void mySubTask(){   
  2.     ...   
  3.     try{   
  4.         sleep(delay);   
  5.     }catch(InterruptedException e){}//不要这样做   
  6.     ...   
  7.  

void mySubTask(){

...

try{

 sleep(delay);

}catch(InterruptedException e){}//不要这样做

...

}

 

如果你不知道抛InterruptedException异常后如何处理,那么你有如下好的建议处理方式:
1
、在catch子句中,调用Thread.currentThread.interrupt()来设置中断状态(因为抛出异常后中断标示会被清除),让外界通过判断Thread.currentThread().isInterrupted()标示来决定是否终止线程还是继续下去,应该这样做:

Java代码

  1. void mySubTask() {   
  2.     ...   
  3.     try {   
  4.         sleep(delay);   
  5.     } catch (InterruptedException e) {   
  6.         Thread.currentThread().isInterrupted();   
  7.     }   
  8.     ...   
  9. }  

void mySubTask() {

...

try {

 sleep(delay);

} catch (InterruptedException e) {

 Thread.currentThread().isInterrupted();

}

...

}

2、或者,更好的做法就是,不使用try来捕获这样的异常,让方法直接抛出:

Java代码

  1. void mySubTask() throws InterruptedException {   
  2.     ...   
  3.     sleep(delay);   
  4.     ...   
  5.  

void mySubTask() throws InterruptedException {

...

sleep(delay);

...

}

 

中断应用

使用中断信号量中断非阻塞状态的线程

中断线程最好的,最受推荐的方式是,使用共享变量(shared variable)发出信号,告诉线程必须停止正在运行的任务。线程必须周期性的核查这一变量,然后有秩序地中止任务。Example2描述了这一方式:

Java代码

  1. class Example2 extends Thread {   
  2.     volatile boolean stop = false;// 线程中断信号量   
  3.   
  4.     public static void main(String args[]) throws Exception {   
  5.         Example2 thread = new Example2();   
  6.         System.out.println("Starting thread...");   
  7.         thread.start();   
  8.         Thread.sleep(3000);   
  9.         System.out.println("Asking thread to stop...");   
  10.         // 设置中断信号量   
  11.         thread.stop = true;   
  12.         Thread.sleep(3000);   
  13.         System.out.println("Stopping application...");   
  14.     }   
  15.   
  16.     public void run() {   
  17.         // 每隔一秒检测一下中断信号量   
  18.         while (!stop) {   
  19.             System.out.println("Thread is running...");   
  20.             long time = System.currentTimeMillis();   
  21.             /*  
  22.              * 使用while循环模拟 sleep 方法,这里不要使用sleep,否则在阻塞时会   
  23.              * InterruptedException异常而退出循环,这样while检测stop条件就不会执行,  
  24.              * 失去了意义。  
  25.              */  
  26.             while ((System.currentTimeMillis() - time < 1000)) {}   
  27.         }   
  28.         System.out.println("Thread exiting under request...");   
  29.     }   
  30. }  

class Example2 extends Thread {

volatile boolean stop = false;//线程中断信号量

 

public static void main(String args[]) throws Exception {

 Example2 thread = new Example2();

 System.out.println("Starting thread...");

 thread.start();

 Thread.sleep(3000);

 System.out.println("Asking thread to stop...");

 // 设置中断信号量

 thread.stop = true;

 Thread.sleep(3000);

 System.out.println("Stopping application...");

}

 

public void run() {

 // 每隔一秒检测一下中断信号量

 while (!stop) {

  System.out.println("Thread is running...");

  long time = System.currentTimeMillis();

  /*

   * 使用while循环模拟 sleep方法,这里不要使用sleep,否则在阻塞时会 抛

   * InterruptedException异常而退出循环,这样while检测stop条件就不会执行,

   * 失去了意义。

   */

  while ((System.currentTimeMillis() - time < 1000)) {}

 }

 System.out.println("Thread exiting under request...");

}

}

 

使用thread.interrupt()中断非阻塞状态线程

虽然Example2该方法要求一些编码,但并不难实现。同时,它给予线程机会进行必要的清理工作。这里需注意一点的是需将共享变量定义成volatile类型或将对它的一切访问封入同步的块/方法(synchronized blocks/methods)中。上面是中断一个非阻塞状态的线程的常见做法,但对非检测isInterrupted()条件会更简洁:

Java代码

  1. class Example2 extends Thread {   
  2.     public static void main(String args[]) throws Exception {   
  3.         Example2 thread = new Example2();   
  4.         System.out.println("Starting thread...");   
  5.         thread.start();   
  6.         Thread.sleep(3000);   
  7.         System.out.println("Asking thread to stop...");   
  8.         // 发出中断请求   
  9.         thread.interrupt();   
  10.         Thread.sleep(3000);   
  11.         System.out.println("Stopping application...");   
  12.     }   
  13.   
  14.     public void run() {   
  15.         // 每隔一秒检测是否设置了中断标示   
  16.         while (!Thread.currentThread().isInterrupted()) {   
  17.             System.out.println("Thread is running...");   
  18.             long time = System.currentTimeMillis();   
  19.             // 使用while循环模拟 sleep   
  20.             while ((System.currentTimeMillis() - time < 1000) ) {   
  21.             }   
  22.         }   
  23.         System.out.println("Thread exiting under request...");   
  24.     }   
  25. }  

class Example2 extends Thread {

public static void main(String args[]) throws Exception {

 Example2 thread = new Example2();

 System.out.println("Starting thread...");

 thread.start();

 Thread.sleep(3000);

 System.out.println("Asking thread to stop...");

 // 发出中断请求

 thread.interrupt();

 Thread.sleep(3000);

 System.out.println("Stopping application...");

}

 

public void run() {

 // 每隔一秒检测是否设置了中断标示

 while (!Thread.currentThread().isInterrupted()) {

  System.out.println("Thread is running...");

  long time = System.currentTimeMillis();

  // 使用while循环模拟 sleep

  while ((System.currentTimeMillis() - time < 1000) ) {

  }

 }

 System.out.println("Thread exiting under request...");

}

}

 

到目前为止一切顺利!但是,当线程等待某些事件发生而被阻塞,又会发生什么?当然,如果线程被阻塞,它便不能核查共享变量,也就不能停止。这在许多情况下会发生,例如调用Object.wait()ServerSocket.accept()DatagramSocket.receive()时,这里仅举出一些。

 

他们都可能永久的阻塞线程。即使发生超时,在超时期满之前持续等待也是不可行和不适当的,所以,要使用某种机制使得线程更早地退出被阻塞的状态。下面就来看一下中断阻塞线程技术。

使用thread.interrupt()中断阻塞状态线程

Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,设置线程的中断标示位,在线程受到阻塞的地方(如调用sleepwaitjoin等地方)抛出一个异常InterruptedException,并且中断状态也将被清除,这样线程就得以退出阻塞的状态。下面是具体实现:

 

Java代码

  1. class Example3 extends Thread {   
  2.     public static void main(String args[]) throws Exception {   
  3.         Example3 thread = new Example3();   
  4.         System.out.println("Starting thread...");   
  5.         thread.start();   
  6.         Thread.sleep(3000);   
  7.         System.out.println("Asking thread to stop...");   
  8.         thread.interrupt();// 等中断信号量设置后再调用   
  9.         Thread.sleep(3000);   
  10.         System.out.println("Stopping application...");   
  11.     }   
  12.   
  13.     public void run() {   
  14.         while (!Thread.currentThread().isInterrupted()) {   
  15.             System.out.println("Thread running...");   
  16.             try {   
  17.                 /*  
  18.                  * 如果线程阻塞,将不会去检查中断信号量stop变量,所 thread.interrupt()  
  19.                  * 会使阻塞线程从阻塞的地方抛出异常,让阻塞线程从阻塞状态逃离出来,并  
  20.                  * 进行异常块进行 相应的处理  
  21.                  */  
  22.                 Thread.sleep(1000);// 线程阻塞,如果线程收到中断操作信号将抛出异常   
  23.             } catch (InterruptedException e) {   
  24.                 System.out.println("Thread interrupted...");   
  25.                 /*  
  26.                  * 如果线程在调用 Object.wait()方法,或者该类的 join() sleep()方法  
  27.                  * 过程中受阻,则其中断状态将被清除  
  28.                  */  
  29.                 System.out.println(this.isInterrupted());// false   
  30.   
  31.                 //中不中断由自己决定,如果需要真真中断线程,则需要重新设置中断位,如果   
  32.                 //不需要,则不用调用   
  33.                 Thread.currentThread().interrupt();   
  34.             }   
  35.         }   
  36.         System.out.println("Thread exiting under request...");   
  37.     }   
  38. }  

class Example3 extends Thread {

public static void main(String args[]) throws Exception {

 Example3 thread = new Example3();

 System.out.println("Starting thread...");

 thread.start();

 Thread.sleep(3000);

 System.out.println("Asking thread to stop...");

 thread.interrupt();// 等中断信号量设置后再调用

 Thread.sleep(3000);

 System.out.println("Stopping application...");

}

 

public void run() {

 while (!Thread.currentThread().isInterrupted()) {

  System.out.println("Thread running...");

  try {

   /*

    * 如果线程阻塞,将不会去检查中断信号量stop变量,所以thread.interrupt()

    * 会使阻塞线程从阻塞的地方抛出异常,让阻塞线程从阻塞状态逃离出来,并

    * 进行异常块进行 相应的处理

    */

   Thread.sleep(1000);// 线程阻塞,如果线程收到中断操作信号将抛出异常

  } catch (InterruptedException e) {

   System.out.println("Thread interrupted...");

   /*

    * 如果线程在调用 Object.wait()方法,或者该类的 join()sleep()方法

    * 过程中受阻,则其中断状态将被清除

    */

   System.out.println(this.isInterrupted());// false

 

   //中不中断由自己决定,如果需要真真中断线程,则需要重新设置中断位,如果

   //不需要,则不用调用

   Thread.currentThread().interrupt();

  }

 }

 System.out.println("Thread exiting under request...");

}

}

 

一旦Example3中的Thread.interrupt()被调用,线程便收到一个异常,于是逃离了阻塞状态并确定应该停止。上面我们还可以使用共享信号量来替换!Thread.currentThread().isInterrupted()条件,但不如它简洁。

死锁状态线程无法被中断

Example4试着去中断处于死锁状态的两个线程,但这两个线都没有收到任何中断信号(抛出异常),所以interrupt()方法是不能中断死锁线程的,因为锁定的位置根本无法抛出异常:

Java代码

  1. class Example4 extends Thread {   
  2.     public static void main(String args[]) throws Exception {   
  3.         final Object lock1 = new Object();   
  4.         final Object lock2 = new Object();   
  5.         Thread thread1 = new Thread() {   
  6.             public void run() {   
  7.                 deathLock(lock1, lock2);   
  8.             }   
  9.         };   
  10.         Thread thread2 = new Thread() {   
  11.             public void run() {   
  12.                 // 注意,这里在交换了一下位置   
  13.                 deathLock(lock2, lock1);   
  14.             }   
  15.         };   
  16.         System.out.println("Starting thread...");   
  17.         thread1.start();   
  18.         thread2.start();   
  19.         Thread.sleep(3000);   
  20.         System.out.println("Interrupting thread...");   
  21.         thread1.interrupt();   
  22.         thread2.interrupt();   
  23.         Thread.sleep(3000);   
  24.         System.out.println("Stopping application...");   
  25.     }   
  26.   
  27.     static void deathLock(Object lock1, Object lock2) {   
  28.         try {   
  29.             synchronized (lock1) {   
  30.                 Thread.sleep(10);// 不会在这里死掉   
  31.                 synchronized (lock2) {// 会锁在这里,虽然阻塞了,但不会抛异常   
  32.                     System.out.println(Thread.currentThread());   
  33.                 }   
  34.             }   
  35.         } catch (InterruptedException e) {   
  36.             e.printStackTrace();   
  37.             System.exit(1);   
  38.         }   
  39.     }   
  40. }  

class Example4 extends Thread {

public static void main(String args[]) throws Exception {

 final Object lock1 = new Object();

 final Object lock2 = new Object();

 Thread thread1 = new Thread() {

  public void run() {

   deathLock(lock1, lock2);

  }

 };

 Thread thread2 = new Thread() {

  public void run() {

   // 注意,这里在交换了一下位置

   deathLock(lock2, lock1);

  }

 };

 System.out.println("Starting thread...");

 thread1.start();

 thread2.start();

 Thread.sleep(3000);

 System.out.println("Interrupting thread...");

 thread1.interrupt();

 thread2.interrupt();

 Thread.sleep(3000);

 System.out.println("Stopping application...");

}

 

static void deathLock(Object lock1, Object lock2) {

 try {

  synchronized (lock1) {

   Thread.sleep(10);// 不会在这里死掉

   synchronized (lock2) {// 会锁在这里,虽然阻塞了,但不会抛异常

    System.out.println(Thread.currentThread());

   }

  }

 } catch (InterruptedException e) {

  e.printStackTrace();

  System.exit(1);

 }

}

}

 

中断I/O操作

然而,如果线程在I/O操作进行时被阻塞,又会如何?I/O操作可以阻塞线程一段相当长的时间,特别是牵扯到网络应用时。例如,服务器可能需要等待一个请求(request),又或者,一个网络应用程序可能要等待远端主机的响应。

 

实现此InterruptibleChannel接口的通道是可中断的:如果某个线程在可中断通道上因调用某个阻塞的 I/O操作(常见的操作一般有这些:serverSocketChannel. accept()socketChannel.connectsocketChannel.opensocketChannel.readsocketChannel.writefileChannel.readfileChannel.write)而进入阻塞状态,而另一个线程又调用了该阻塞线程的 interrupt 方法,这将导致该通道被关闭,并且已阻塞线程接将会收到ClosedByInterruptException,并且设置已阻塞线程的中断状态。另外,如果已设置某个线程的中断状态并且它在通道上调用某个阻塞的 I/O操作,则该通道将关闭并且该线程立即接收到 ClosedByInterruptException;并仍然设置其中断状态。如果情况是这样,其代码的逻辑和第三个例子中的是一样的,只是异常不同而已。

 

如果你正使用通道(channels)(这是在Java 1.4中引入的新的I/O API),那么被阻塞的线程将收到一个ClosedByInterruptException异常。但是,你可能正使用Java1.0之前就存在的传统的I/O,而且要求更多的工作。既然这样,Thread.interrupt()将不起作用,因为线程将不会退出被阻塞状态。Example5描述了这一行为。尽管interrupt()被调用,线程也不会退出被阻塞状态,比如ServerSocketaccept方法根本不抛出异常。

 

很幸运,Java平台为这种情形提供了一项解决方案,即调用阻塞该线程的套接字的close()方法。在这种情形下,如果线程被I/O操作阻塞,当调用该套接字的close方法时,该线程在调用accept地方法将接收到一个SocketExceptionSocketExceptionIOException的子异常)异常,这与使用interrupt()方法引起一个InterruptedException异常被抛出非常相似,(注,如果是流因读写阻塞后,调用流的close方法也会被阻塞,根本不能调用,更不会抛IOExcepiton,此种情况下怎样中断?我想可以转换为通道来操作流可以解决,比如文件通道)。下面是具体实现:

Java代码

  1. class Example6 extends Thread {   
  2.     volatile ServerSocket socket;   
  3.   
  4.     public static void main(String args[]) throws Exception {   
  5.         Example6 thread = new Example6();   
  6.         System.out.println("Starting thread...");   
  7.         thread.start();   
  8.         Thread.sleep(3000);   
  9.         System.out.println("Asking thread to stop...");   
  10.         Thread.currentThread().interrupt();// 再调用interrupt方法   
  11.         thread.socket.close();// 再调用close方法   
  12.         try {   
  13.             Thread.sleep(3000);   
  14.         } catch (InterruptedException e) {   
  15.         }   
  16.         System.out.println("Stopping application...");   
  17.     }   
  18.   
  19.     public void run() {   
  20.         try {   
  21.             socket = new ServerSocket(8888);   
  22.         } catch (IOException e) {   
  23.             System.out.println("Could not create the socket...");   
  24.             return;   
  25.         }   
  26.         while (!Thread.currentThread().isInterrupted()) {   
  27.             System.out.println("Waiting for connection...");   
  28.             try {   
  29.                 socket.accept();   
  30.             } catch (IOException e) {   
  31.                 System.out.println("accept() failed or interrupted...");   
  32.                 Thread.currentThread().interrupt();//重新设置中断标示位   
  33.             }   
  34.         }   
  35.         System.out.println("Thread exiting under request...");   
  36.     }   
  37. }  class Example6 extends Thread {volatile ServerSocket socket;

 

 public static void main(String args[]) throws Exception {

  Example6 thread = new Example6();

  System.out.println("Starting thread...");

  thread.start();

  Thread.sleep(3000);

  System.out.println("Asking thread to stop...");

  Thread.currentThread().interrupt();// 再调用interrupt方法

  thread.socket.close();// 再调用close方法

  try {

    Thread.sleep(3000);

  } catch (InterruptedException e) {

  }

  System.out.println("Stopping application...");

 }

 

 public void run() {

  try {

    socket = new ServerSocket(8888);

  } catch (IOException e) {

    System.out.println("Could not create the socket...");

    return;

  }

  while (!Thread.currentThread().isInterrupted()) {

    System.out.println("Waiting for connection...");

    try {

     socket.accept();

    } catch (IOException e) {

     System.out.println("accept() failed or interrupted...");

     Thread.currentThread().interrupt();//重新设置中断标示位

    }

  }

  System.out.println("Thread exiting under request...");

 }

}