【发布时间】:2012-09-30 02:25:02
【问题描述】:
在 Callable 中处理 Thread.interrupted() 的正确方法是什么?我猜这个callable应该抛出一个InterruptedException;例如:
public class MyCallable implements Callable<Object> {
public Object call() {
Object result = null;
// Simulate long-running operation that calculates result
while (true) {
...
if (Thread.interrupted()) {
throw new InterruptedException();
}
}
result = ... // something produced by the long-running operation
return result;
}
}
这是正确的,还是有更合适的方法来处理它?谢谢。
【问题讨论】:
-
解释
InterruptedException的一个真正来源是here。 -
我之前读过,但它没有提到 Callable。不过,非常好的文章。
标签: java multithreading future callable