java总是调用线程。currentThread()。中断();当捕捉到中断异常时?
This IBM developerWorks article表示:
“The one time it is acceptable to swallow an interrupt is when you know the thread is about to exit. This scenario only occurs when the class calling the interruptible method is part of a
Thread
, not aRunnable
[…]”.
到现在为止,我总是为我的线程实现Runnable
。给出如下Runnable
实现:
public class View() implements Runnable {
@Overload
public void run(){
Thread worker = new Thread(new Worker());
worker.start();
do{
try{
TimeUnit.SECONDS.sleep(3);
updateView();
}catch(InterruptedException e){
worker.interrupt();
// Thread.currentThread().interrupt();
return;
}
}while(true);
}
protected void updateView(){
// …
}
}
真的有必要在我的return;
语句之前调用Thread.currentThread().interrupt();
吗?难道return;
还没有执行干净的enaugh退出吗?叫它有什么好处?这篇文章指出应该这样做,因为否则“[…]调用堆栈上更高级别的代码将无法找到它[…]”。在Thread.State.TERMINATED
中设置中断标志的线程在应用程序关闭时不设置中断标志有什么好处?你能给我一个例子吗Runnable
之外的代码出于合理的原因检查中断标志
顺便问一下,扩展Thread
而不是实现Runnable
是更好的代码设计吗
共 (0) 个答案