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

简介: 先看三个方法原型: public void interrupt(); public boolean isInterrupted(); public static boolean interrupted();  一、先说interrupt()方法,看注释 Interrupts this thread.

 

先看三个方法原型:
 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()方法,看源码:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  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();看源码:
   

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  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)。
 注意:这个是个静态方法,并且返回的是当前线程状态,并不一定是调用者的线程状态

 

看例子:
 例一:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  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()一行

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  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

 

例三:自己中断自己

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  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方法

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
 
  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://stackoverflow.com/questions/13623445/future-cancel-method-is-not-working

 

相关文章
|
16天前
|
Java
在Java多线程编程中,实现Runnable接口通常优于继承Thread类
【10月更文挑战第20天】在Java多线程编程中,实现Runnable接口通常优于继承Thread类。原因包括:1) Java只支持单继承,实现接口不受此限制;2) Runnable接口便于代码复用和线程池管理;3) 分离任务与线程,提高灵活性。因此,实现Runnable接口是更佳选择。
28 2
|
16天前
|
Java 开发者
在Java多线程编程中,创建线程的方法有两种:继承Thread类和实现Runnable接口
【10月更文挑战第20天】在Java多线程编程中,创建线程的方法有两种:继承Thread类和实现Runnable接口。本文揭示了这两种方式的微妙差异和潜在陷阱,帮助你更好地理解和选择适合项目需求的线程创建方式。
13 3
|
2月前
|
存储 Java 程序员
优化Java多线程应用:是创建Thread对象直接调用start()方法?还是用个变量调用?
这篇文章探讨了Java中两种创建和启动线程的方法,并分析了它们的区别。作者建议直接调用 `Thread` 对象的 `start()` 方法,而非保持强引用,以避免内存泄漏、简化线程生命周期管理,并减少不必要的线程控制。文章详细解释了这种方法在使用 `ThreadLocal` 时的优势,并提供了代码示例。作者洛小豆,文章来源于稀土掘金。
|
6月前
|
Java 程序员 调度
Thread类及常见方法
Thread类及常见方法
|
6月前
|
Java 程序员 API
多线程Thread(初阶二:Thread类及常⻅⽅法)
多线程Thread(初阶二:Thread类及常⻅⽅法)
56 0
|
6月前
|
资源调度 调度
Thread的基本方法(3)-yield方法的分析与实例说明
Thread的基本方法(3)-yield方法的分析与实例说明
66 0
|
Java 调度
Thread类的方法
Thread类的方法
41 0
|
调度
Thread 类的基本方法
Thread 类的基本方法
84 0
|
调度
Thread 类中的 yield()方法有什么作用?
Thread 类中的 yield()方法有什么作用?
153 0