interrupt(),interrupted() 和 is

  1. 结论先行

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

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

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

现在对各方法逐一进行具体介绍:

  1. interrupt()

首先我们来使用一下 interrupt() 方法,观察效果,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
复制代码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);
}
}
}

输出结果:

image

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

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

  1. interrupted() 和 isInterrupted()

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
复制代码@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");
}

输出结果:

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 对象。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
复制代码@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。

参考文章:www.cnblogs.com/hapjin/p/54…

PS:本文原创发布于微信公众号「不只Java」。

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%