[英]Impossible to interrupt thread if its actually computing?
鉴于此代码......
public class SimpleTest {
@Test
public void testCompletableFuture() throws Exception {
Thread thread = new Thread(SimpleTest::longOperation);
thread.start();
bearSleep(1);
thread.interrupt();
bearSleep(5);
}
public static void longOperation(){
System.out.println("started");
try {
boolean b = true;
while (true) {
b = !b;
}
}catch (Exception e){
System.out.println("exception happened hurray!");
}
System.out.println("completed");
}
private static void bearSleep(long seconds){
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {}
}
}
想象一下,而不是这个while(true)
你有一些不会抛出中断执行的东西(例如,一个实际计算某事的递归函数)。
你怎么杀这个东西? 为什么它不会死?
请注意,如果我没有在那里放置Exception
类型并使用InterruptedException
它甚至不会编译,说"interrupted exception will never be thrown"
,我不明白为什么。 也许我想手动打断它...
我假设你指的是这段代码:
try {
boolean b = true;
while (true) {
b = !b;
}
} catch(Exception e) {
System.out.println("exception happened hurray!");
}
你不能在这里捕获InterruptedException
的原因是因为该块内部没有任何内容可以抛出InterruptedException
。 interrupt()
本身不会将线程从循环中断开,相反,它本质上会向线程发送一个信号,告诉它停止它正在做什么并做其他事情。 如果你想interrupt()
来打破循环,试试这个:
boolean b = true;
while (true) {
b = !b;
// Check if we got interrupted.
if(Thread.interrupted()) {
break; // Break out of the loop.
}
}
现在线程将检查它是否被中断,并且一旦它有中断就退出循环。 没有必要的try-catch
。
Thread#interrupt
或多或少用标志实现。 如果线程在某些操作上被阻止,例如:IO或同步原语(请参阅Javadoc),则线程被解除阻塞,并在该线程中抛出InterruptedException
。 否则,设置一个简单的状态标志,指示线程被中断。
您的代码需要使用Thread#interrupted()
或Thread#isInterrupted()
(或处理InterruptedException
)来检查该状态。
请注意,使用static
方法检查状态会清除状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.