关于Thread类中三个interrupt方法的研究与学习(转)

 

先看三个方法原型:
 public void interrupt();
 public boolean isInterrupted();
 public static boolean interrupted();

 一、先说interrupt()方法,看注释
 Interrupts this thread.

Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

If this thread is blocked in an I/O operation upon an interruptible channel then the channel will be closed, the thread's interrupt status will be set, and the thread will receive a java.nio.channels.ClosedByInterruptException.

If this thread is blocked in a java.nio.channels.Selector then the thread's interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector's wakeup method were invoked.

If none of the previous conditions hold then this thread's interrupt status will be set.

Interrupting a thread that is not alive need not have any effect.

Throws:
SecurityException - if the current thread cannot modify this thread

 意思是说,当这个线程刚好或即将被阻塞在wait,join,sleep方法的时候,调用这个方法会引起这个线程的interrupt状态被清空(设为false),并且前者三个方法会抛出InterruptedException。
 除此之外(这个线程不处于wait,join,sleep方法),这个线程的interrupt状态会被设置(设为true)。

 

 二、isInterrupted()方法,看源码:

  1.  public boolean isInterrupted() {  
  2.  return isInterrupted(false);  
  3. }  
  4.  private native boolean isInterrupted(boolean ClearInterrupted);  


 

  看注释:
  Tests whether this thread has been interrupted. The interrupted status of the thread is unaffected by this method. 


  意思是说:返回这个线程是否被interrupt了,调用这个方法不会影响这个线程的interrupt状态

 

  三、public static boolean interrupted();看源码:
   

  1. public static boolean interrupted() {  
  2.   return currentThread().isInterrupted(true);  
  3.  }  
  4.  private native boolean isInterrupted(boolean ClearInterrupted);  


 

 看注释:
 Tests whether the current thread has been interrupted. The interrupted status of the thread is cleared by this method. 
 In other words, if this method were to be called twice in succession, the second call would return false (unless the current thread were interrupted again, after the first call had cleared its interrupted status and before the second call had examined it). 


 意思是说:调用这个方法会返回当前线程的interrupt状态(true或false),并把当前线程的interrupt状态清空(设为false)。
 注意:这个是个静态方法,并且返回的是当前线程状态,并不一定是调用者的线程状态

 

看例子:
 例一:

  1. @Test  
  2.     public void test1(){  
  3.         Thread t1= new Thread(){  
  4.             @Override  
  5.             public void run() {  
  6.                 System.out.println("begin");  
  7.                 for(int i=0;i<100;i++){  
  8.                     System.out.println("i="+i+" "+this.isInterrupted());  
  9.                       
  10.                     try {  
  11.                         Socket socket= new Socket("10.22.1.115",23);//不存在的ip,让这句话执行时间长一些,方便看效果  
  12.                     } catch (UnknownHostException e1) {  
  13.                         // TODO Auto-generated catch block  
  14.                         e1.printStackTrace();  
  15.                     } catch (IOException e1) {  
  16.                         // TODO Auto-generated catch block  
  17.                         e1.printStackTrace();  
  18.                     }  
  19.                     System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());  
  20.                     System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());  
  21.                     System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());  
  22.                     try {  
  23.                         Thread.sleep(5000);  
  24.                     } catch (InterruptedException e) {  
  25.                         // TODO Auto-generated catch block  
  26.                     //  e.printStackTrace();  
  27.                         System.out.println("exception:"+this.isInterrupted());  
  28.                         System.out.println("exception:"+Thread.interrupted());  
  29.                     //  return;  
  30.                     }  
  31.                 }  
  32.                 System.out.println("end");  
  33.             }  
  34.         };  
  35.           
  36.         t1.start();  
  37.           
  38.         try {  
  39.             Thread.sleep(3000);  
  40.         } catch (InterruptedException e) {  
  41.             // TODO Auto-generated catch block  
  42.             e.printStackTrace();  
  43.         }  
  44.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  45.           
  46.           
  47.         System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());  
  48.         t1.interrupt();  
  49.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  50.         System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());  
  51.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  52.           
  53.           
  54.           
  55.         try {  
  56.             Thread.sleep(100000);  
  57.         } catch (InterruptedException e) {  
  58.             // TODO Auto-generated catch block  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  


执行结果:

begin
i=0 false
false  false
begin interrupt  t1.isInterrupted():false
false  false
end interrupt  t1.isInterrupted():true
false  false
java.net.ConnectException: Connection timed out: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at java.net.Socket.<init>(Socket.java:375)
 at java.net.Socket.<init>(Socket.java:189)
 at baby.thread.InterruptTest$1.run(InterruptTest.java:21)
i=0  this.isInterrupted():true
i=0  Thread.interrupted():true
i=0  this.isInterrupted():false
休息5秒钟
i=1 false
…………

 

例二:注释掉Thread.interrupted()一行

  1. @Test  
  2.     public void test13(){  
  3.         Thread t1= new Thread(){  
  4.             @Override  
  5.             public void run() {  
  6.                 System.out.println("begin");  
  7.                 for(int i=0;i<100;i++){  
  8.                     System.out.println("i="+i+" "+this.isInterrupted());  
  9.                       
  10.                     try {  
  11.                         Socket socket= new Socket("10.22.1.115",23);  
  12.                     } catch (UnknownHostException e1) {  
  13.                         // TODO Auto-generated catch block  
  14.                         e1.printStackTrace();  
  15.                     } catch (IOException e1) {  
  16.                         // TODO Auto-generated catch block  
  17.                         e1.printStackTrace();  
  18.                     }  
  19.                     System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());  
  20.                 //  System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());  
  21.                     System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());  
  22.                     try {  
  23.                         Thread.sleep(5000);  
  24.                     } catch (InterruptedException e) {  
  25.                         // TODO Auto-generated catch block  
  26.                         //  e.printStackTrace();  
  27.                         System.out.println("exception:"+this.isInterrupted());  
  28.                         System.out.println("exception:"+Thread.interrupted());  
  29.                         //  return;  
  30.                     }  
  31.                 }  
  32.                 System.out.println("end");  
  33.             }  
  34.         };  
  35.           
  36.         t1.start();  
  37.           
  38.         try {  
  39.             Thread.sleep(3000);  
  40.         } catch (InterruptedException e) {  
  41.             // TODO Auto-generated catch block  
  42.             e.printStackTrace();  
  43.         }  
  44.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  45.           
  46.           
  47.         System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());  
  48.         t1.interrupt();  
  49.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  50.         System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());  
  51.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  52.           
  53.           
  54.           
  55.         try {  
  56.             Thread.sleep(100000);  
  57.         } catch (InterruptedException e) {  
  58.             // TODO Auto-generated catch block  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  


输出:
 begin
i=0 false
false  false
begin interrupt  t1.isInterrupted():false
false  false
end interrupt  t1.isInterrupted():true
false  false
java.net.ConnectException: Connection timed out: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at java.net.Socket.<init>(Socket.java:375)
 at java.net.Socket.<init>(Socket.java:189)
 at baby.thread.InterruptTest$2.run(InterruptTest.java:82)
i=0  this.isInterrupted():true
i=0  this.isInterrupted():true
没有休息5秒,直接下面输出
exception:false
exception:false
i=1 false

 

例三:自己中断自己

  1. @Test  
  2.     public void test12(){  
  3.         Thread t1= new Thread(){  
  4.             @Override  
  5.             public void run() {  
  6.                 System.out.println("begin");  
  7.                 for(int i=0;i<100;i++){  
  8.                     System.out.println("i="+i+" "+this.isInterrupted());  
  9.                     interrupt();  
  10.                     try {  
  11.                         Socket socket= new Socket("10.22.1.115",23);  
  12.                     } catch (UnknownHostException e1) {  
  13.                         // TODO Auto-generated catch block  
  14.                         e1.printStackTrace();  
  15.                     } catch (IOException e1) {  
  16.                         // TODO Auto-generated catch block  
  17.                         e1.printStackTrace();  
  18.                     }  
  19.                     System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());  
  20.                     System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());  
  21.                     System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());  
  22.                     try {  
  23.                         Thread.sleep(5000);  
  24.                     } catch (InterruptedException e) {  
  25.                         // TODO Auto-generated catch block  
  26.                         //  e.printStackTrace();  
  27.                         System.out.println("exception:"+this.isInterrupted());  
  28.                         System.out.println("exception:"+Thread.interrupted());  
  29.                         //  return;  
  30.                     }  
  31.                 }  
  32.                 System.out.println("end");  
  33.             }  
  34.         };  
  35.           
  36.         t1.start();  
  37.           
  38.         try {  
  39.             Thread.sleep(3000);  
  40.         } catch (InterruptedException e) {  
  41.             // TODO Auto-generated catch block  
  42.             e.printStackTrace();  
  43.         }  
  44.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  45.           
  46.           
  47.         System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());  
  48.     //  t1.interrupt();  
  49.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  50.         System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());  
  51.         System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  52.           
  53.           
  54.           
  55.         try {  
  56.             Thread.sleep(100000);  
  57.         } catch (InterruptedException e) {  
  58.             // TODO Auto-generated catch block  
  59.             e.printStackTrace();  
  60.         }  
  61.     }  


结果输出和例一一样:
 begin
i=0 false
false  false
begin interrupt  t1.isInterrupted():true
false  false
end interrupt  t1.isInterrupted():true
false  false
java.net.ConnectException: Connection timed out: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
 at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
 at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
 at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
 at java.net.Socket.connect(Socket.java:529)
 at java.net.Socket.connect(Socket.java:478)
 at java.net.Socket.<init>(Socket.java:375)
 at java.net.Socket.<init>(Socket.java:189)
 at baby.thread.InterruptTest$3.run(InterruptTest.java:143)
i=0  this.isInterrupted():true
i=0  Thread.interrupted():true
i=0  this.isInterrupted():false
休息5秒
i=1 false

 

例四:中断时处于sleep方法

  1. @Test  
  2.     public void test14(){  
  3.         Thread t1= new Thread(){  
  4.             @Override  
  5.             public void run() {  
  6.                 System.out.println("begin");  
  7.                 for(int i=0;i<100;i++){  
  8.                     System.out.println("i="+i+" "+this.isInterrupted());  
  9.                       
  10.                     System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());  
  11. //                  System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());  
  12.                     System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());  
  13.                     try {  
  14.                         Thread.sleep(5000);  
  15.                     } catch (InterruptedException e) {  
  16.                         // TODO Auto-generated catch block  
  17.                         //  e.printStackTrace();  
  18.                         System.out.println("exception:"+this.isInterrupted());  
  19.                         System.out.println("exception:"+Thread.interrupted());  
  20.                         //  return;  
  21.                     }  
  22.                 }  
  23.                 System.out.println("end");  
  24.             }  
  25.         };  
  26.           
  27.         t1.start();  
  28.           
  29.         try {  
  30.             Thread.sleep(3000);  
  31.         } catch (InterruptedException e) {  
  32.             // TODO Auto-generated catch block  
  33.             e.printStackTrace();  
  34.         }  
  35.     //  System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  36.           
  37.           
  38.         System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());  
  39.         t1.interrupt();  
  40.         //System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  41.         System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());  
  42.     //  System.out.println(t1.interrupted()+"  "+Thread.interrupted());  
  43.           
  44.           
  45.           
  46.         try {  
  47.             Thread.sleep(100000);  
  48.         } catch (InterruptedException e) {  
  49.             // TODO Auto-generated catch block  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  

结果输出:
 begin
i=0 false
i=0  this.isInterrupted():false
i=0  this.isInterrupted():false
begin interrupt  t1.isInterrupted():false
没有休息5秒
exception:false
exception:false
i=1 false
i=1  this.isInterrupted():false
i=1  this.isInterrupted():false
end interrupt  t1.isInterrupted():false

 

http://blog.csdn.net/chaofanwei/article/details/19157747

http://www.cnblogs.com/hanyuan/archive/2013/03/10/2952229.html

http://*.com/questions/13623445/future-cancel-method-is-not-working

 

上一篇:为了转行程序员而努力的第三十天-栈


下一篇:《Python算法教程》——2.8 参考资料