在Callable中處理Thread.interrupted()的正確方法是什麼?我猜測可調用應該拋出一個InterruptedException;例如:在Callable中處理Thread.interrupted()的正確方法?
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](http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html)。 –
我之前讀過,但沒有提及Callable。不過,非常好的文章。 –