interrupt(),interrupted() 和 isInterrupted() 的区别

1. 结论先行

interrupt():将调用该方法的对象所表示的线程标记一个中止标记,并非真的中止该线程。

interrupted():获取当前线程的中断状态,而且会清除线程的状态标记。是一个是静态方法。html

isInterrupted():获取调用该方法的对象所表示的线程,不会清除线程的状态标记。是一个实例方法。微信

如今对各方法逐一进行具体介绍:ide

2. interrupt()

首先咱们来使用一下 interrupt() 方法,观察效果,代码以下:测试

public class MainTest {
   @Test
   public void test() {
       try {
           MyThread01 myThread = new MyThread01();
           myThread.start();
           myThread.sleep(2000);
           myThread.interrupt();
       } catch (Exception e) {
           System.out.println("main catch");
           e.printStackTrace();
       }
   }
}

public class MyThread01 extends Thread {
   @Override
   public void run() {
       super.run();
       for (int i = 0; i < 500; i++) {
           System.out.println("i= " + i);
       }
   }
}

输出结果:spa

image

能够看出,子线程已经执行完成了。说明 interrupt() 方法是不能让线程中止,和咱们一开始所说的那样,它仅仅是在当前线程记下一个中止标记而已。线程

那么这个中止标记咱们又怎么知道呢?——此时就要介绍下面的 interrupted() 和 isInterrupted() 方法了。code

3. interrupted() 和 isInterrupted()

  • interrupted() 方法的声明为 public static boolean interrupted()
  • isInterrupted() 方法的声明为 public boolean isInterrupted()

这两个方法很类似,下面咱们用程序来看下使用效果上的区别吧htm

先来看下使用 interrupted() 的程序。对象

@Test
public void test() {
      try {
          MyThread01 myThread = new MyThread01();
          myThread.start();
          myThread.sleep(1000);
//     7行: Thread.currentThread().interrupt(); // Thread.currentThread() 这里表示 main 线程
          myThread.interrupt();
          // myThread.interrupted() 底层调用了 currentThread().isInterrupted(true); 做用是判断当前线程是否为中止状态
          System.out.println("是否中断1 " + myThread.interrupted());
          System.out.println("是否中断2 " + myThread.interrupted());
      } catch (InterruptedException e) {
          System.out.println("main catch");
      }
  System.out.println("main end");
}

输出结果:blog

image

由此能够看出,线程并未中止,同时也证实了 interrupted() 方法的解释:测试当前线程是否已经中断,这个当前线程就是 main 线程,它从未中断过,因此打印结果都是 false。

那么如何使 main 线程产生中断效果呢?将上面第 8 行代码注释掉,并将第 7 行代码的注释去掉再运行,咱们就能够获得如下输出结果:

image

从结果上看,方法 interrupted() 的确判断出了当前线程(此例为 main 线程)是不是中止状态了,但为何第二个布尔值为 false 呢?咱们在最开始的时候有说过——interrupted() 测试当前线程是否已是中断状态,执行后会将状态标志清除。

由于执行 interrupted() 后它会将状态标志清除,底层调用了 isInterrupted(true),此处参数为 true 。因此 interrupted() 具备清除状态标记功能。

在第一次调用时,因为此前执行了 Thread.currentThread().interrupt();,致使当前线程被标记了一个中断标记,所以第一次调用 interrupted() 时返回 true。由于 interrupted() 具备清除状态标记功能,因此在第二次调用 interrupted() 方法时会返回 false。

以上就是 interrupted() 的介绍内容,最后咱们再来看下 isInterrupted() 方法吧。

isInterrupted() 和 interrupted() 有两点不一样:一是不具备清除状态标记功能,由于底层传入 isInterrupted() 方法的参数为 false。二是它判断的线程调用该方法的对象所表示的线程,本例为 MyThread01 对象。

咱们修改一下上面的代码,看下运行效果:

@Test
public void test() {
   try {
       MyThread01 myThread = new MyThread01();
       myThread.start();
       myThread.sleep(1000);
       myThread.interrupt();
       // 修改了下面这两行。
       // 上面的代码是 myThread.interrupted();
       System.out.println("是否中断1 " + myThread.isInterrupted());
       System.out.println("是否中断2 " + myThread.isInterrupted());
   } catch (InterruptedException e) {
       System.out.println("main catch");
       e.printStackTrace();
   }
   System.out.println("main end");
}

输出结果:

image

结果很明显,由于 isInterrupted() 不具备清除状态标记功能,因此两次都输出 true。

参考文章:http://www.cnblogs.com/hapjin...

欢迎关注微信公众号「不仅Java」,后台回复「电子书」,送说不定有你想要的呢
图片描述

相关文章
相关标签/搜索