先看三个方法原型:
 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. try {
  10. Socket socket= new Socket("10.22.1.115",23);//不存在的ip,让这句话执行时间长一些,方便看效果
  11. } catch (UnknownHostException e1) {
  12. // TODO Auto-generated catch block
  13. e1.printStackTrace();
  14. } catch (IOException e1) {
  15. // TODO Auto-generated catch block
  16. e1.printStackTrace();
  17. }
  18. System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
  19. System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());
  20. System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
  21. try {
  22. Thread.sleep(5000);
  23. } catch (InterruptedException e) {
  24. // TODO Auto-generated catch block
  25. //  e.printStackTrace();
  26. System.out.println("exception:"+this.isInterrupted());
  27. System.out.println("exception:"+Thread.interrupted());
  28. //  return;
  29. }
  30. }
  31. System.out.println("end");
  32. }
  33. };
  34. t1.start();
  35. try {
  36. Thread.sleep(3000);
  37. } catch (InterruptedException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  42. System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());
  43. t1.interrupt();
  44. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  45. System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());
  46. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  47. try {
  48. Thread.sleep(100000);
  49. } catch (InterruptedException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. }

执行结果:

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. try {
  10. Socket socket= new Socket("10.22.1.115",23);
  11. } catch (UnknownHostException e1) {
  12. // TODO Auto-generated catch block
  13. e1.printStackTrace();
  14. } catch (IOException e1) {
  15. // TODO Auto-generated catch block
  16. e1.printStackTrace();
  17. }
  18. System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
  19. //  System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());
  20. System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
  21. try {
  22. Thread.sleep(5000);
  23. } catch (InterruptedException e) {
  24. // TODO Auto-generated catch block
  25. //  e.printStackTrace();
  26. System.out.println("exception:"+this.isInterrupted());
  27. System.out.println("exception:"+Thread.interrupted());
  28. //  return;
  29. }
  30. }
  31. System.out.println("end");
  32. }
  33. };
  34. t1.start();
  35. try {
  36. Thread.sleep(3000);
  37. } catch (InterruptedException e) {
  38. // TODO Auto-generated catch block
  39. e.printStackTrace();
  40. }
  41. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  42. System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());
  43. t1.interrupt();
  44. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  45. System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());
  46. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  47. try {
  48. Thread.sleep(100000);
  49. } catch (InterruptedException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. }

输出:
 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. t1.start();
  36. try {
  37. Thread.sleep(3000);
  38. } catch (InterruptedException e) {
  39. // TODO Auto-generated catch block
  40. e.printStackTrace();
  41. }
  42. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  43. System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());
  44. //  t1.interrupt();
  45. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  46. System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());
  47. System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  48. try {
  49. Thread.sleep(100000);
  50. } catch (InterruptedException e) {
  51. // TODO Auto-generated catch block
  52. e.printStackTrace();
  53. }
  54. }

结果输出和例一一样:
 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. System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
  10. //                  System.out.println("i="+i+"  Thread.interrupted():"+Thread.interrupted());
  11. System.out.println("i="+i+"  this.isInterrupted():"+this.isInterrupted());
  12. try {
  13. Thread.sleep(5000);
  14. } catch (InterruptedException e) {
  15. // TODO Auto-generated catch block
  16. //  e.printStackTrace();
  17. System.out.println("exception:"+this.isInterrupted());
  18. System.out.println("exception:"+Thread.interrupted());
  19. //  return;
  20. }
  21. }
  22. System.out.println("end");
  23. }
  24. };
  25. t1.start();
  26. try {
  27. Thread.sleep(3000);
  28. } catch (InterruptedException e) {
  29. // TODO Auto-generated catch block
  30. e.printStackTrace();
  31. }
  32. //  System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  33. System.out.println("begin interrupt  t1.isInterrupted():"+t1.isInterrupted());
  34. t1.interrupt();
  35. //System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  36. System.out.println("end interrupt  t1.isInterrupted():"+t1.isInterrupted());
  37. //  System.out.println(t1.interrupted()+"  "+Thread.interrupted());
  38. try {
  39. Thread.sleep(100000);
  40. } catch (InterruptedException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. }

结果输出:
 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

关于Thread类中三个interrupt方法的研究与学习(转)的更多相关文章

  1. 【Java_多线程并发编程】基础篇—Thread类中start()和run()方法的区别

    1. start() 和 run()的区别说明 start()方法: 它会启动一个新线程,并将其添加到线程池中,待其获得CPU资源时会执行run()方法,start()不能被重复调用. run()方法 ...

  2. 正确停止线程的方式三 使用Thread类中的内置的中断标记位-----------不熟悉

    package charpter10; public class Processor implements Runnable { @Override public void run() { for ( ...

  3. Java线程状态及Thread类中的主要方法

    要想实现多线程,就必须在主线程中创建新的线程对象. 不论什么线程一般具有5种状态,即创建,就绪,执行,堵塞,终止. 创建状态: 在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态,此时 ...

  4. Thread类中的join方法

    package charpter06; //类实现接口public class Processor implements Runnable { // 重写接口方法 @Override public v ...

  5. 多线程(Thread类中的方法线程名称)

    1 package multithread; 2 3 /* 4 * 如何创建一个线程呢? 5 * 6 * 创建线程方式一:继承Thread类. 7 * 8 * 步骤: 9 * 1,定义一个类继承Thr ...

  6. java 多线程3:Thread类中的静态方法

    Thread类中的静态方法 Thread类中的静态方法表示操作的线程是"正在执行静态方法所在的代码块的线程".为什么Thread类中要有静态方法,这样就能对CPU当前正在运行的线程 ...

  7. UnSafe类中的一些重要方法

    UnSafe类中的一些重要方法 JDK中的rt.jar保重Unsafe类中提供了硬件级别的原子性操作,Unsafe类中的方法都是navtice方法,他们使用JNI的方式访问C++实现库,下面我们来了解 ...

  8. Python之路(第四十二篇)线程相关的其他方法、join()、Thread类的start()和run()方法的区别、守护线程

    一.线程相关的其他方法 Thread实例对象的方法 # isAlive(): 返回线程是否活动的. # getName(): 返回线程名. # setName(): 设置线程名. ​ threadin ...

  9. python: 面向对象:类和对象调用类中的变量和方法

    一. 面向对象初识 我们在生活中做事都是面向过程的,前面实现一些基本逻辑功能代码也是用面向过程的语句实现的,后来学了函数,把这些功能又装到了函数里.但用面向过程的方法去写程序,只能实现一个功能,我们要 ...

随机推荐

  1. 【转】文件恢复神器extundelete

    参考博文: 1.Linux中VMware虚拟机增加磁盘空间的扩容操作 http://www.net130.com/CMS/Pub/special/special_virtual/special_vir ...

  2. Android studio gradle配置

    什么是Gradle? Gradle是一种依赖管理工具,基于Groovy语言,面向Java应用为主,它抛弃了基于XML的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. gr ...

  3. iOS开发中捕获Crash 发送Bug邮件

    在开发过程中,我们有时候会留下Bug,用户在使用我们的app 的时候,有时会出现闪退,这时候我们可以让用户给我们发送邮件,以让我们开发人员更加快速的地位到Bug的所在,以最快的时间解决,同时也提高用户 ...

  4. PHP - 接口&抽象类

    什么时候使用抽象类什么时候使用接口? .如果要创建一个模型,这个模型将由一些紧密相关的对象采用,就可以使用抽象类.如果要创建将由一些不相关对象采用的功能,就使用接口. .如果必须从多个来源继承行为,就 ...

  5. BZOJ 2190: [SDOI2008]仪仗队( 欧拉函数 )

    假设C君为(0, 0), 则右上方为(n - 1, n - 1). 一个点(x, y) 能被看到的前提是gcd(x, y) = 1, 所以 answer = ∑ phi(i) * 2 + 2 - 1 ...

  6. android绑定Service失败原因

    今天抄一个代码,学习Service,中间Service的绑定一直是失败的. bindService返回false 上网查询的话都是一些,比如说TabHost的问题 发现和自己的问题不一样. 最后想了想 ...

  7. Matlab,Visio等生成的图片的字体嵌入问题解决方法

    确保所有字体嵌入,是生成高质量学术论文的必要条件.但是在Windows下,总会遇到Matlab或Visio生成字体没有嵌入的问题,当然这个问题的解决办法有很多(例如,对于Visio可以这样做:直接拷贝 ...

  8. MPMoviePlayerController导致statusBar消失,导致内存泄露leak

    1.MPMoviePlayerController使statusBar消失 同事写项目时,运行程序总导致statusBar状态条消失,然后就是界面会上移20个像素,导致最下面空白界面,找原因一直不知道 ...

  9. [置顶] 程序员面试之道(《程序员面试笔试宝典》)之看着别人手拿大把的offer,不淡定了怎么办?

    不管是在哪里,不管发生什么事,不要随便放下自己. ——<当男人恋爱时> 很多求职者都会面临一个问题:别人手拿大把大把的offer了,而自己却是两手空空,别人签约之后已经过着“猪狗不如”的悠 ...

  10. 【模式识别】Boosting

    Boosting简单介绍 分类中通常使用将多个弱分类器组合成强分类器进行分类的方法,统称为集成分类方法(Ensemble Method).比較简单的如在Boosting之前出现Bagging的方法,首 ...