亲宝软件园·资讯

展开

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

爱吃南瓜糕的北络 人气:0

一、代码

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

@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());
}

加载全部内容

相关教程
猜你喜欢
用户评论