interrupted() 和 isInterrupted() 的区别

Thread 类中提供了两种方法用来判断线程的状态是不是停止的。就是我们今天的两位主人公 interrupted() 和 isInterrupted() 。

interrupted()

image_1as4prvqmf31unj13asakincvm.png-21.5kB

官方解释:测试当前线程是否已经中断,当前线程是指运行 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

image_1as4q86481d2u10uo1q3enfnagk1t.png-8.8kB

程序运行结束后,结果如上图所示,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

image_1as4qopti13m4lqqivjb3imf12a.png-7.6kB

从上述结果中可以看出,方法 interrupted() 的确判断出当前线程是否已经停止,但是为什么第 2 个布尔值是 false 呢?让我们重新阅读一次文档。

image_1as4qsebm1pvo1tv21laq1kqb1jap2n.png-21.6kB

你看,文档中说的很详细,interrupted() 方法具有清除状态的功能,所以第二次的时候返回值是 false。


isInterrupted()

image_1as4psmlbgfhhhgmm31as71dnp13.png-16.1kB

从声明中可以看出来 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

image_1as4sik741tdg96f1t7n75g1do43h.png-4.3kB


总结

1. interrupted():测试 当前线程 是否已经是中断状态,执行后具有清除状态功能

2. isInterrupted():测试线程 Thread 对象 是否已经是中断状态,但不清楚状态标志

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值