为什么我打印两次 thread.isInterrupted(),结果是 ture false?
来源:5-14 interrupt状态

战侠歌sy
2019-09-30
public class RightWayInterrupted{
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Thread1());
thread.start();
thread.interrupt();
System.out.println(thread.isInterrupted()); System.out.println(thread.isInterrupted());
thread.join();
} }
class Thread1 implements Runnable{ @Override public void run() { } }
写回答
3回答
-
涛滔浪
2020-04-02
线程结束运行了的话,thread.isInterrupted()返回false。所以所有可能结果是:1. true,true;2. true,false;3. false,false。你后面加的join讲道理没啥用。你的run方法什么东西都没有,所以可能运行到这行代码前就结束了。
112020-09-20 -
张婧仪
2023-04-19
是因为打印第二次的时候,run方法已经运行完毕了,所以返回false。
你可以做个实验,加入Thread.sleep(5000),打印都是false
实验如下:
00 -
悟空
2019-09-30
我运行你的这个代码,是两次true。
你是不是运行的代码不是贴的这个?贴一下完整代码吧。
00
相似问题