In threads when dealing with cancelation, you often you see code like this
while (!shutdown) {
.. do something, if a blocking call, then it will throw the interrupted exception
try { .. some more ... }
catch (InterruptedException e) {
shutdown = true;
}
}
What I want to know is, is this, or why is this, better than doing this
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
}
The way I see it is that in the latter case you don't need to bother with the shutdown var at all.