java中有两个代码块。
区块一:
@Test
public void test1() {
System.out.println("interrupt:" + Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt();
System.out.println("interrupt:" + Thread.currentThread().isInterrupted());
}
输出:
interrupt:false
interrupt:true
第二块:
@Test
public void test2() throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("running...");
}
});
thread.interrupt();
TimeUnit.SECONDS.sleep(2);
System.out.println("interrupt:" + thread.isInterrupted());
thread.start();
TimeUnit.SECONDS.sleep(2);
System.out.println("interrupt:" + thread.isInterrupted());
}
输出:
interrupt:false
running...
interrupt:false
所以,我的问题:
interrupt:true
为什么在调用后阻止一个打印interrupt()
但不阻止两个?- JVM调用interrupt()后会做什么?
谢谢!
PS:第三块:
@Test
public void test3() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("running...");
}
});
thread.interrupt();
System.out.println("interrupt:" + thread.isInterrupted());
// thread.start();
//
// thread.interrupt();
//
//
// System.out.println("interrupt:" + thread.isInterrupted());
}
还输出:interrupt:false