我是Java世界的新手,如果问题很蠢请见谅。
最近我在Runnable对象的run()方法中看到了以下代码:
try {
if (Thread.interrupted()) {
throw new InterruptedException();
}
// do something
if (Thread.interrupted()) {
throw new InterruptedException();
}
// do something
if (Thread.interrupted()) {
throw new InterruptedException();
}
// and so on
} catch (InterruptedException e){
// Handle exception
} finally {
// release resource
}
您应该经常检查线程中断,并在何处进行检查,有哪些好的实践方法?
thread.interrupt()
时发生的事情。 - jtahlborn