用interrupt()中断Java线程

来源:互联网 发布:券商研究员 知乎 编辑:程序博客网 时间:2024/06/06 11:49
最近在学习Java线程相关的东西,和大家分享一下,有错误之处欢迎大家指正. 

假如我们有一个任务如下,交给一个Java线程来执行,如何才能保证调用interrupt()来中断它呢? 
Java代码  收藏代码
  1. class ATask implements Runnable{  
  2.   
  3.     private double d = 0.0;  
  4.       
  5.     public void run() {  
  6.         //死循环执行打印"I am running!" 和做消耗时间的浮点计算  
  7.         while (true) {  
  8.             System.out.println("I am running!");  
  9.               
  10.             for (int i = 0; i < 900000; i++) {  
  11.                 d =  d + (Math.PI + Math.E) / d;  
  12.             }  
  13.             //给线程调度器可以切换到其它进程的信号  
  14.             Thread.yield();  
  15.         }  
  16.     }  
  17. }  
  18.   
  19. public class InterruptTaskTest {  
  20.       
  21.     public static void main(String[] args) throws Exception{  
  22.         //将任务交给一个线程执行  
  23.         Thread t = new Thread(new ATask());  
  24.         t.start();  
  25.           
  26.         //运行一断时间中断线程  
  27.         Thread.sleep(100);  
  28.         System.out.println("****************************");  
  29.         System.out.println("Interrupted Thread!");  
  30.         System.out.println("****************************");  
  31.         t.interrupt();  
  32.     }  
  33. }   


运行这个程序,我们发现调用interrupt()后,程序仍在运行,如果不强制结束,程序将一直运行下去,如下所示: 
Java代码  收藏代码
  1. ......  
  2. I am running!  
  3. I am running!  
  4. I am running!  
  5. I am running!  
  6. ****************************  
  7. Interrupted Thread!  
  8. ****************************  
  9. I am running!  
  10. I am running!  
  11. I am running!  
  12. I am running!  
  13. I am running!  
  14. ....  

虽然中断发生了,但线程仍然在进行,离开线程有两种常用的方法: 
抛出InterruptedException和用Thread.interrupted()检查是否发生中断,下面分别看一下这两种方法: 
1.在阻塞操作时如Thread.sleep()时被中断会抛出InterruptedException(注意,进行不能中断的IO操作而阻塞和要获得对象的锁调用对象的synchronized方法而阻塞时不会抛出InterruptedException) 
Java代码  收藏代码
  1. class ATask implements Runnable{  
  2.   
  3.     private double d = 0.0;  
  4.       
  5.     public void run() {  
  6.         //死循环执行打印"I am running!" 和做消耗时间的浮点计算  
  7.         try {  
  8.             while (true) {  
  9.                 System.out.println("I am running!");  
  10.                   
  11.                 for (int i = 0; i < 900000; i++) {  
  12.                     d =  d + (Math.PI + Math.E) / d;  
  13.                 }  
  14.                 //休眠一断时间,中断时会抛出InterruptedException  
  15.                 Thread.sleep(50);  
  16.             }  
  17.         } catch (InterruptedException e) {  
  18.             System.out.println("ATask.run() interrupted!");  
  19.         }  
  20.     }  
  21. }  

程序运行结果如下: 
Java代码  收藏代码
  1. I am running!  
  2. I am running!  
  3. ****************************  
  4. Interrupted Thread!  
  5. ****************************  
  6. ATask.run() interrupted!  

可以看到中断任务时让任务抛出InterruptedException来离开任务. 

2.Thread.interrupted()检查是否发生中断.Thread.interrupted()能告诉你线程是否发生中断,并将清除中断状态标记,所以程序不会两次通知你线程发生了中断. 
Java代码  收藏代码
  1. class ATask implements Runnable{  
  2.   
  3.     private double d = 0.0;  
  4.       
  5.     public void run() {  
  6.           
  7.         //检查程序是否发生中断  
  8.         while (!Thread.interrupted()) {  
  9.             System.out.println("I am running!");  
  10.   
  11.             for (int i = 0; i < 900000; i++) {  
  12.                 d = d + (Math.PI + Math.E) / d;  
  13.             }  
  14.         }  
  15.   
  16.         System.out.println("ATask.run() interrupted!");  
  17.     }  
  18. }  

程序运行结果如下: 
Java代码  收藏代码
  1. I am running!  
  2. I am running!  
  3. I am running!  
  4. I am running!  
  5. I am running!  
  6. I am running!  
  7. I am running!  
  8. ****************************  
  9. Interrupted Thread!  
  10. ****************************  
  11. ATask.run() interrupted!  


我们可结合使用两种方法来达到可以通过interrupt()中断线程.请看下面例子: 
Java代码  收藏代码
  1. class ATask implements Runnable{  
  2.   
  3.     private double d = 0.0;  
  4.       
  5.     public void run() {  
  6.           
  7.         try {  
  8.         //检查程序是否发生中断  
  9.         while (!Thread.interrupted()) {  
  10.             System.out.println("I am running!");  
  11.             //point1 before sleep  
  12.             Thread.sleep(20);  
  13.             //point2 after sleep  
  14.             System.out.println("Calculating");  
  15.             for (int i = 0; i < 900000; i++) {  
  16.                 d = d + (Math.PI + Math.E) / d;  
  17.             }  
  18.         }  
  19.           
  20.         } catch (InterruptedException e) {  
  21.             System.out.println("Exiting by Exception");  
  22.         }  
  23.           
  24.         System.out.println("ATask.run() interrupted!");  
  25.     }  
  26. }  

在point1之前处point2之后发生中断会产生两种不同的结果,可以通过修改InterruptTaskTest main()里的Thread.sleep()的时间来达到在point1之前产生中断或在point2之后产生中断. 
如果在point1之前发生中断,程序会在调用Thread.sleep()时抛出InterruptedException从而结束线程.这和在Thread.sleep()时被中断是一样的效果.程序运行结果可能如下: 
Java代码  收藏代码
  1. I am running!  
  2. Calculating  
  3. I am running!  
  4. Calculating  
  5. I am running!  
  6. Calculating  
  7. I am running!  
  8. ****************************  
  9. Interrupted Thread!  
  10. ****************************  
  11. Exiting by Exception  
  12. ATask.run() interrupted!  

如果在point2之后发生中断,线程会继续执行到下一次while判断中断状态时.程序运行结果可能如下: 
Java代码  收藏代码
  1. I am running!  
  2. Calculating  
  3. I am running!  
  4. Calculating  
  5. I am running!  
  6. Calculating  
  7. ****************************  
  8. Interrupted Thread!  
  9. ****************************  
  10. ATask.run() interrupted!  

在JAVA中,通过其对线程类的内嵌支持,编程人员编写多线程程序是很简易的。然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决,将导致意外的行为以及细微的、难以发现的错误。
      在本篇文章中,我们针对这些难题之一:如何中断一个正在运行的线程。 
                                                                                    
背景
    中断(Interrupt)一个线程意味着在该线程完成任务之前停止其正在进行的一切,有效地中止其当前的操作。线程是死亡、还是等待新的任务或是继续运行至下一步,就取决于这个程序。 虽然初次看来它可能显得简单,但是,你必须进行一些预警以实现期望的结果。你最好还是牢记以下的几点告诫。

    首先,忘掉Thread.stop方法。虽然它确实停止了一个正在运行的线程,然而,这种方法是不安全也是不受提倡的,这意味着,在未来的JAVA版本中,它将不复存在。

    一些轻率的家伙可能被另一种方法Thread.interrupt所迷惑。尽管,其名称似乎在暗示着什么,然而,这种方法并不会中断一个正在运行的线程(待会将进一步说明),正如Listing A中描述的那样。它创建了一个线程,并且试图使用Thread.interrupt方法停止该线程。Thread.sleep()方法的调用,为线程的初始化和中止提供了充裕的时间。线程本身并不参与任何有用的操作。

Listing A

[java] view plaincopy
  1. class Example1 extends Thread {  
  2.     boolean stop = false;  
  3.     public static void main(String args[]) throws Exception {  
  4.         Example1 thread = new Example1();  
  5.         System.out.println("Starting thread...");  
  6.         thread.start();  
  7.         Thread.sleep(3000);  
  8.         System.out.println("Interrupting thread...");  
  9.         thread.interrupt();  
  10.         Thread.sleep(3000);  
  11.         System.out.println("Stopping application...");  
  12.         // System.exit(0);  
  13.     }  
  14.     public void run() {  
  15.         while (!stop) {  
  16.             System.out.println("Thread is running...");  
  17.             long time = System.currentTimeMillis();  
  18.             while ((System.currentTimeMillis() - time < 1000)) {  
  19.             }  
  20.         }  
  21.         System.out.println("Thread exiting under request...");  
  22.     }  
  23. }  

    如果你运行了Listing A中的代码,你将在控制台看到以下输出:

    Starting thread...

    Thread is running...

    Thread is running...

    Thread is running...

    Interrupting thread...

    Thread is running...

    Thread is running...

    Thread is running...

    Stopping application...

    Thread is running...

    Thread is running...

    Thread is running...
    ............................... 
    甚至,在Thread.interrupt()被调用后,线程仍然继续运行。

    真正地中断一个线程

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

    Listing B

    1. class  Example2  extends  Thread {
    2.      volatile   boolean  stop =  false ;
    3.      public   static   void  main(String args[])  throws  Exception {
    4.         Example2 thread =  new  Example2();
    5.         System.out.println( "Starting thread..." );
    6.         thread.start();
    7.         Thread.sleep( 3000 );
    8.         System.out.println( "Asking thread to stop..." );
    9.         thread.stop =  true ;
    10.         Thread.sleep( 3000 );
    11.         System.out.println( "Stopping application..." );
    12.          // System.exit( 0 );
    13.     }
    14.      public   void  run() {
    15.          while  (!stop) {
    16.             System.out.println( "Thread is running..." );
    17.              long  time = System.currentTimeMillis();
    18.              while  ((System.currentTimeMillis() - time <  1000 ) && (!stop)) {
    19.             }
    20.         }
    21.         System.out.println( "Thread exiting under request..." );
    22.     }
    23. }

    运行Listing B中的代码将产生如下输出(注意线程是如何有秩序的退出的)

    Starting thread...

    Thread is running...

    Thread is running...

    Thread is running...

    Asking thread to stop...

    Thread exiting under request...

    Stopping application...

       虽然该方法要求一些编码,但并不难实现。同时,它给予线程机会进行必要的清理工作,这在任何一个多线程应用程序中都是绝对需要的。请确认将共享变量定义成volatile 类型或将对它的一切访问封入同步的块/方法(synchronized blocks/methods)中。

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

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

    很不幸运,不存在这样一种机制对所有的情况都适用,但是,根据情况不同却可以使用特定的技术。在下面的环节,我将解答一下最普遍的例子。

    使用Thread.interrupt()中断线程

      正如Listing A中所描述的,Thread.interrupt()方法不会中断一个正在运行的线程。这一方法实际上完成的是,在线程受到阻塞时抛出一个中断信号,这样线程就得以退出阻塞的状态。更确切的说,如果线程被Object.wait, Thread.join和Thread.sleep三种方法之一阻塞,那么,它将接收到一个中断异常(InterruptedException),从而提早地终结被阻塞状态。

        因此,如果线程被上述几种方法阻塞,正确的停止线程方式是设置共享变量,并调用interrupt()(注意变量应该先设置)。如果线程没有被阻塞,这时调用interrupt()将不起作用;否则,线程就将得到异常(该线程必须事先预备好处理此状况),接着逃离阻塞状态。在任何一种情况中,最后线程都将检查共享变量然后再停止。Listing C这个示例描述了该技术。

    Listing C

    1. class  Example3  extends  Thread {
    2.      volatile   boolean  stop =  false ;
    3.      public   static   void  main(String args[])  throws  Exception {
    4.         Example3 thread =  new  Example3();
    5.         System.out.println( "Starting thread..." );
    6.         thread.start();
    7.         Thread.sleep( 3000 );
    8.         System.out.println( "Asking thread to stop..." );
    9.         thread.stop =  true ; // 如果线程阻塞,将不会检查此变量
    10.         thread.interrupt();
    11.         Thread.sleep( 3000 );
    12.         System.out.println( "Stopping application..." );
    13.          // System.exit( 0 );
    14.     }
    15.      public   void  run() {
    16.          while  (!stop) {
    17.             System.out.println( "Thread running..." );
    18.              try  {
    19.                 Thread.sleep( 1000 );
    20.             }  catch  (InterruptedException e) {
    21.                 System.out.println( "Thread interrupted..." );
    22.             }
    23.         }
    24.         System.out.println( "Thread exiting under request..." );
    25.     }
    26. }

    一旦Listing C中的Thread.interrupt()被调用,线程便收到一个异常,于是逃离了阻塞状态并确定应该停止。运行以上代码将得到下面的输出:

    Starting thread...

    Thread running...

    Thread running...

    Thread running...

    Asking thread to stop...

    Thread interrupted...

    Thread exiting under request...

    Stopping application...


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

    如果你正使用通道(channels)(这是在Java 1.4中引入的新的I/O API),那么被阻塞的线程将收到一个ClosedByInterruptException异常。如果情况是这样,其代码的逻辑和第三个例子中的是一样的,只是异常不同而已。

    但是,你可能正使用Java1.0之前就存在的传统的I/O,而且要求更多的工作。既然这样,Thread.interrupt()将不起作用,因为线程将不会退出被阻塞状态。Listing D描述了这一行为。尽管interrupt()被调用,线程也不会退出被阻塞状态

    Listing D

    1. import  java.io.*;
    2. import  java.net.ServerSocket;
    3. import  java.net.Socket;
    4. class  Example4  extends  Thread {
    5.      public   static   void  main(String args[])  throws  Exception {
    6.         Example4 thread =  new  Example4();
    7.         System.out.println( "Starting thread..." );
    8.         thread.start();
    9.         Thread.sleep( 3000 );
    10.         System.out.println( "Interrupting thread..." );
    11.         thread.interrupt();
    12.         Thread.sleep( 3000 );
    13.         System.out.println( "Stopping application..." );
    14.          // System.exit( 0 );
    15.     }
    16.      public   void  run() {
    17.         ServerSocket socket;
    18.          try  {
    19.             socket =  new  ServerSocket( 7856 );
    20.         }  catch  (IOException e) {
    21.             System.out.println( "Could not create the socket..." );
    22.              return ;
    23.         }
    24.          while  ( true ) {
    25.             System.out.println( "Waiting for connection..." );
    26.              try  {
    27.                 Socket sock = socket.accept();
    28.             }  catch  (IOException e) {
    29.                 System.out.println( "accept() failed or interrupted..." );
    30.             }
    31.         }
    32.     }
    33. }

       很幸运,Java平台为这种情形提供了一项解决方案,即调用阻塞该线程的套接字的close()方法。在这种情形下,如果线程被I/O操作阻塞,该线程将接收到一个SocketException异常,这与使用interrupt()方法引起一个InterruptedException异常被抛出非常相似。

    唯一要说明的是,必须存在socket的引用(reference),只有这样close()方法才能被调用。这意味着socket对象必须被共享。Listing E描述了这一情形。运行逻辑和以前的示例是相同的。

    Listing E

    1. import  java.net.*;
    2. import  java.io.*;
    3. class  Example5  extends  Thread {
    4.      volatile   boolean  stop =  false ;
    5.      volatile  ServerSocket socket;
    6.      public   static   void  main(String args[])  throws  Exception {
    7.         Example5 thread =  new  Example5();
    8.         System.out.println( "Starting thread..." );
    9.         thread.start();
    10.         Thread.sleep( 3000 );
    11.         System.out.println( "Asking thread to stop..." );
    12.         thread.stop =  true ;
    13.         thread.socket.close();
    14.         Thread.sleep( 3000 );
    15.         System.out.println( "Stopping application..." );
    16.          // System.exit( 0 );
    17.     }
    18.      public   void  run() {
    19.          try  {
    20.             socket =  new  ServerSocket( 7856 );
    21.         }  catch  (IOException e) {
    22.             System.out.println( "Could not create the socket..." );
    23.              return ;
    24.         }
    25.          while  (!stop) {
    26.             System.out.println( "Waiting for connection..." );
    27.              try  {
    28.                 Socket sock = socket.accept();
    29.             }  catch  (IOException e) {
    30.                 System.out.println( "accept() failed or interrupted..." );
    31.             }
    32.         }
    33.         System.out.println( "Thread exiting under request..." );
    34.     }
    35. }

    以下是运行Listing E中代码后的输出:

    Starting thread...

    Waiting for connection...

    Asking thread to stop...

    accept() failed or interrupted...

    Thread exiting under request...

    Stopping application...

    多线程是一个强大的工具,然而它正呈现出一系列难题。其中之一是如何中断一个正在运行的线程。如果恰当地实现,使用上述技术中断线程将比使用Java平台上已经提供的内嵌操作更为简单。


    0 0
    原创粉丝点击