[英]Impossible to interrupt thread if its actually computing?
Given this code... 鉴于此代码......
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) {}
}
}
Imagine that instead of this while(true)
you have something that does not throw interrupted execution (for example, a recursive function that actually calculates something). 想象一下,而不是这个
while(true)
你有一些不会抛出中断执行的东西(例如,一个实际计算某事的递归函数)。
How do you kill this thing? 你怎么杀这个东西? And why is it not dying?
为什么它不会死?
Note if I don't put Exception
type there and use InterruptedException
it won't even compile, saying that "interrupted exception will never be thrown"
which I don't understand why. 请注意,如果我没有在那里放置
Exception
类型并使用InterruptedException
它甚至不会编译,说"interrupted exception will never be thrown"
,我不明白为什么。 Maybe I want to interrupt it manually... 也许我想手动打断它...
I assume you are referring to this segment of code: 我假设你指的是这段代码:
try {
boolean b = true;
while (true) {
b = !b;
}
} catch(Exception e) {
System.out.println("exception happened hurray!");
}
The reason you cannot catch an InterruptedException
here is because there is nothing inside of that block that can throw an InterruptedException
. 你不能在这里捕获
InterruptedException
的原因是因为该块内部没有任何内容可以抛出InterruptedException
。 interrupt()
itself will not break the thread out of the loop, instead, it essentially sends a signal to the thread to tell it to stop what it's doing and do something else. interrupt()
本身不会将线程从循环中断开,相反,它本质上会向线程发送一个信号,告诉它停止它正在做什么并做其他事情。 If you want interrupt()
to break the loop, try this: 如果你想
interrupt()
来打破循环,试试这个:
boolean b = true;
while (true) {
b = !b;
// Check if we got interrupted.
if(Thread.interrupted()) {
break; // Break out of the loop.
}
}
Now the thread will check if it got interrupted, and break out of the loop once it has. 现在线程将检查它是否被中断,并且一旦它有中断就退出循环。 No
try-catch
necessary. 没有必要的
try-catch
。
Thread#interrupt
is more or less implemented with a flag. Thread#interrupt
或多或少用标志实现。 If the thread is blocked on some actions, ex: IO or synchronization primitives (see Javadoc), the thread is unblocked and an InterruptedException
, is thrown in that thread. 如果线程在某些操作上被阻止,例如:IO或同步原语(请参阅Javadoc),则线程被解除阻塞,并在该线程中抛出
InterruptedException
。 Otherwise, a simple status flag is set indicating that the thread was interrupted. 否则,设置一个简单的状态标志,指示线程被中断。
Your code needs to check for that status with Thread#interrupted()
or Thread#isInterrupted()
(or handle the InterruptedException
). 您的代码需要使用
Thread#interrupted()
或Thread#isInterrupted()
(或处理InterruptedException
)来检查该状态。
Note that checking for the status with the static
method clears the status. 请注意,使用
static
方法检查状态会清除状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.