java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java thread.isInterrupted()

Java thread.isInterrupted() 返回值不确定结果分析解决

作者:爱吃南瓜糕的北络

这篇文章主要介绍了Java thread.isInterrupted() 返回值不确定结果分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

一、代码

先上代码(以下这段代码会有多种执行结果)

@Test
public void test_interrupted_thread() throws Exception {
    InterruptThread interruptThread = new InterruptThread();
    interruptThread.start();
    interruptThread.interrupt();
    System.out.println("interruptThread.isInterrupted():" + interruptThread.isInterrupted());
}
public class InterruptThread extends Thread {
    @Override
    public void run() {
        for (int i=0; i< 3; i++) {
            System.out.println("i=" + (i + 1));
        }
        System.out.println("【InterruptThread】结束");
    }
}

执行结果1:
i=1
i=2
i=3
【Thread-0】【InterruptThread】结束
【main】interruptThread.isInterrupted():false
执行结果2:
【main】interruptThread.isInterrupted():true
i=1
i=2
i=3
【Thread-0】【InterruptThread】结束
执行结果3:
i=1
i=2
i=3
【Thread-0】【InterruptThread】结束
【main】interruptThread.isInterrupted():true

二、分析结果

执行结果1:

Main线程调用了interruptThread.start();,interruptThread线程启动,执行了interruptThread线程内容,同时Main线程调用了interruptThread.interrupt();,设定了interruptThread线程中断标记为true,最后InterruptThread结束,清除中断标记,Main线程调用interruptThread.isInterrupted() 获取interruptThread线程中断标记为false。

执行结果2:

Main线程调用了interruptThread.start();,interruptThread线程启动,但是由于CPU随机调度,在执行了interruptThread线程内容前,先执行Main线程调用interruptThread.interrupt();,设定了interruptThread线程中断标记为true,且先调用interruptThread.isInterrupted()获取interruptThread线程中断标记为true并输出,最后在执行interruptThread线程内容。

执行结果3:

Main线程调用了interruptThread.start();,interruptThread线程启动,执行了interruptThread线程内容,同时Main线程调用了interruptThread.interrupt();,设定了interruptThread线程中断标记为true,最后InterruptThread结束,但是Main线程调用interruptThread.isInterrupted() 获取interruptThread线程中断标记为true。(与执行结果1执行顺序一致,但是最终结果不一致)

原因分析:

Main线程调用interruptThread.interrupt()后立即调用interruptThread.isInterrupted(),虽然interruptThread执行结束,但有可能在interruptThread线程还未完成清除打断标记就Main线程就查看打断标记,此时仍然为true。

三、解决方案

如果Main线程要得到稳定的false,即重置打断标记后的结果,有如下方案:

(1)需要Main线程在调用interruptThread.interrupt();,对Main线程sleep一会,给点时间,再通过调用interruptThread.isInterrupted()获取interruptThread线程的中断状态。

@Test
public void test_interrupted_thread() throws Exception {
    InterruptThread interruptThread = new InterruptThread();
    interruptThread.start();
    interruptThread.interrupt();
    Thread.sleep(100);
    System.out.println("【" + Thread.currentThread().getName() + "】" + "interruptThread.isInterrupted():" + interruptThread.isInterrupted());
}

(2)也可以通过Main线程调用interruptThread.join(),让Main线程等到interruptThread执行直到中止后再调用interruptThread.isInterrupted()获取interruptThread线程的中断状态。

@Test
public void test_interrupted_thread() throws Exception {
    InterruptThread interruptThread = new InterruptThread();
    interruptThread.start();
    interruptThread.interrupt();
    interruptThread.join();
    System.out.println("【" + Thread.currentThread().getName() + "】" + "interruptThread.isInterrupted():" + interruptThread.isInterrupted());
}

到此这篇关于Java thread.isInterrupted() 返回值不确定结果分析解决的文章就介绍到这了,更多相关Java thread.isInterrupted() 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文