キャンセルを処理するスレッドでは、次のようなコードがよく見られます
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
}
私の見方では、後者の場合、shutdownvarをまったく気にする必要はありません。