Java thread.isInterrupted() 返回值不确定结果分析解决
作者:爱吃南瓜糕的北络 时间:2023-11-09 19:27:09
一、代码
先上代码(以下这段代码会有多种执行结果)
@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());
}
来源:https://blog.csdn.net/weixin_37585619/article/details/125674488
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Unity实现俄罗斯方块
![](https://img.aspxhome.com/file/2023/8/69198_0s.jpg)
C#的FileInfo类实现文件操作实例
servlet异步请求的实现
![](https://img.aspxhome.com/file/2023/4/57724_0s.png)
Java SpringBoot实现文件上传功能的示例代码
![](https://img.aspxhome.com/file/2023/1/63631_0s.png)
javafx实现五子棋游戏
![](https://img.aspxhome.com/file/2023/2/129192_0s.png)
spring 中事务注解@Transactional与trycatch的使用
java WebSocket实现聊天消息推送功能
![](https://img.aspxhome.com/file/2023/7/92667_0s.png)
如何在C#9 中使用顶级程序 (top-level)
![](https://img.aspxhome.com/file/2023/2/79052_0s.jpg)
Java实现断点续传功能的示例代码
![](https://img.aspxhome.com/file/2023/2/80832_0s.jpg)
浅谈Java方法调用的优先级问题
完美解决Android Studio集成crashlytics后无法编译的问题
![](https://img.aspxhome.com/file/2023/8/82388_0s.jpg)
基于使用BeginInvoke,EndInvoke异步调用委托的实现代码
SpringBoot自定义注解之实现AOP切面日志详解
![](https://img.aspxhome.com/file/2023/1/99721_0s.jpg)
Androd自定义对话框Dialog视图及参数传递的实现方法
![](https://img.aspxhome.com/file/2023/9/137379_0s.png)
DirectInfo.GetFiles返回数组的默认排序示例
Java实现滑动验证码的示例代码
![](https://img.aspxhome.com/file/2023/0/74890_0s.jpg)
C# XML基础入门小结(XML文件内容增删改查清)
Android库项目中的资源ID冲突的解决方法
Android7.0 工具类:DiffUtil详解
![](https://img.aspxhome.com/file/2023/6/88086_0s.gif)