在处理取消的线程中,您经常会看到这样的代码
while (!shutdown) {
.. do something, if a blocking call, then it will throw the interrupted exception
try { .. some more ... }
catch (InterruptedException e) {
shutdown = true;
}
}
我想知道的是,这是,还是为什么,比这样做更好
try {
while (true) {
.. do something, if a blocking call, then it will throw the interrupted exception
if (Thread.interrupted()) throw new InterruptedException();
}
} catch (InterruptedException e) {
.. clean up, let thread end
}
我的看法是,在后一种情况下,您根本不需要关心 shutdown var。