I am reading Interrupts from Oracle Docs. I am unable to figure out the following part. It states that
What if a thread goes a long time without invoking a method that throws InterruptedException? 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;
}
}
I am scratching my head to understand, what does it mean by What if a thread goes a long time without invoking a method that throws InterruptedException? Secondly, what is the usage of Thread.interrupted(), it is a way, that thread can send a interrupt to itself? Whats the practical usage of this scenario? Thanks.