开发者

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

开发者 https://www.devze.com 2022-12-13 10:33 出处:网络 作者: 爱吃南瓜糕的北络
目录一、代码二、分析结果三、解决方案一、代码 先上代码(以下这段代码会有多种执行结果)
目录
  • 一、代码
  • 二、分析结果
  • 三、解决方案

一、代码

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

@Test
public void test_interrupted_thread() throws Exception {
    InterruptThread interruptThread = new InterruptThread();
    interruptThread.start();
    interruptThread.interrupt();
    Sysphptem.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 +javascript 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_interrupt开发者_JS开发ed_thread() throws Exception {
    InterruptThread interruptThread = new InterruptThread();
    interruptThread.start();
    interruptThread.interrupt();
    in编程terruptThread.join();
    System.out.println("【" + Thread.currentThread().getName() + "】" + "interruptThread.isInterrupted():" + interruptThread.isInterrupted());
}

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

0

精彩评论

暂无评论...
验证码 换一张
取 消