Thread 类中提供了两种方法用来判断线程的状态是不是停止的。就是我们今天的两位主人公 interrupted() 和 isInterrupted() 。
interrupted()
官方解释:测试当前线程是否已经中断,当前线程是指运行 this.interrupted() 方法的线程 。
<span style="color:#000000"><code><span style="color:#000088">public</span> <span style="color:#000088">class</span> <span style="color:#4f4f4f">t12</span> {
<span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">void</span> main(String[] args) {
<span style="color:#000088">try</span> {
MyThread12 thread = <span style="color:#000088">new</span> MyThread12();
thread.start();
Thread.sleep(<span style="color:#006666">500</span>);
thread.interrupt();
System.out.println(<span style="color:#009900">"是否终止1? ="</span> + thread.interrupted());
System.out.println(<span style="color:#009900">"是否终止2? ="</span> + thread.interrupted());
} <span style="color:#000088">catch</span> (InterruptedException e) {
<span style="color:#880000">// TODO Auto-generated catch block</span>
e.printStackTrace();
}
System.out.println(<span style="color:#009900">"-------------end-------------"</span>);
}
}
<span style="color:#000088">class</span> <span style="color:#4f4f4f">MyThread12</span> <span style="color:#000088">extends</span> <span style="color:#4f4f4f">Thread</span> {
<span style="color:#000088">public</span> <span style="color:#000088">void</span> run() {
<span style="color:#000088">for</span> (<span style="color:#000088">int</span> i = <span style="color:#006666">0</span>; i < <span style="color:#006666">50000</span>; i++) {
System.out.println(<span style="color:#009900">"i = "</span> + i);
}
}
}</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
程序运行结束后,结果如上图所示,thread 线程并没有停止,而且调用 thread.interrupted() 结果是两个 false 表示线程一直在运行过程中,为什么会出现这种结果呢?
让我们再来自己看看官方解释,这次请着重阅读我标红的文字:当前线程是指运行 this.interrupted() 方法的线程 。也就是说,当前线程并不是 thread,并不是因为 thread 调用了 interrupted() 方法就是当前线程。当前线程一直是 main 线程,它从未中断过,所以打印结果就是两个 false。
那么如何让 main 线程产生中断效果呢?
<span style="color:#000000"><code><span style="color:#000088">public</span> <span style="color:#000088">class</span> t13 {
<span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">void</span> <span style="color:#009900">main</span>(String[] args) {
Thread.currentThread().interrupt();
System.<span style="color:#000088">out</span>.println(<span style="color:#009900">"是否终止1? ="</span> + Thread.interrupted());
System.<span style="color:#000088">out</span>.println(<span style="color:#009900">"是否终止2? ="</span> + Thread.interrupted());
System.<span style="color:#000088">out</span>.println(<span style="color:#009900">"----------end-----------"</span>);
}
}
</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
从上述结果中可以看出,方法 interrupted() 的确判断出当前线程是否已经停止,但是为什么第 2 个布尔值是 false 呢?让我们重新阅读一次文档。
你看,文档中说的很详细,interrupted() 方法具有清除状态的功能,所以第二次的时候返回值是 false。
isInterrupted()
从声明中可以看出来 isInterrupted() 方法并不是 static 的。并且 isInterrupted() 方法并没有清除状态的功能。
<span style="color:#000000"><code><span style="color:#000088">public</span> <span style="color:#000088">class</span> <span style="color:#4f4f4f">t14</span> {
<span style="color:#000088">public</span> <span style="color:#000088">static</span> <span style="color:#000088">void</span> main(String[] args) {
<span style="color:#000088">try</span> {
MyThread14 thread = <span style="color:#000088">new</span> MyThread14();
thread.start();
Thread.sleep(<span style="color:#006666">1000</span>); <span style="color:#880000">//此方法代表 让当前线程休眠 1 秒,即表示使 main线程休眠 1秒</span>
thread.interrupt();
System.out.println(<span style="color:#009900">"是否终止1? ="</span> + thread.isInterrupted());
System.out.println(<span style="color:#009900">"是否终止2? ="</span> + thread.isInterrupted());
} <span style="color:#000088">catch</span> (InterruptedException e) {
<span style="color:#880000">// TODO Auto-generated catch block</span>
e.printStackTrace();
}
System.out.println(<span style="color:#009900">"----------end-----------"</span>);
}
}
<span style="color:#000088">class</span> <span style="color:#4f4f4f">MyThread14</span> <span style="color:#000088">extends</span> <span style="color:#4f4f4f">Thread</span> {
<span style="color:#000088">public</span> <span style="color:#000088">void</span> run() {
<span style="color:#000088">for</span> (<span style="color:#000088">int</span> i = <span style="color:#006666">0</span>; i < <span style="color:#006666">500000</span>; i++) {
System.out.println(<span style="color:#009900">"i = "</span> + i);
}
}
}</code></span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
总结
1. interrupted():测试 当前线程 是否已经是中断状态,执行后具有清除状态功能。
2. isInterrupted():测试线程 Thread 对象 是否已经是中断状态,但不清楚状态标志。