I want some clarification on thread interruption.
What if a thread goes a long time without invoking a method that throws anInterruptedException? Then it must periodically invoke Thread.interrupted, which returns true if an interrupt has been received. For example:
for (int i = 0; i < inputs.length; i++) {
heavyCrunch(inputs[i]);
if (Thread.interrupted()) {
// We've been interrupted: no more crunching.
return;
}
}
When I call the method Thread.interrupt()
it throws interrupted exception so, why I need to do if (Thread.interrupted())
I will simply do that
try {
for (int i = 0; i < inputs.length; i++) {
heavyCrunch(inputs[i]);
if (Thread.interrupted()) {
// We've been interrupted: no more crunching.
return;
}
}
} catch (InterruptedException ex) {
...
}