You re-interrupt the thread if you need callers to know that an interruption occurred, but you are unable to change your method signature to declare that the method throws InterruptedException
.
For instance, if you are implementing java.lang.Runnable
, you can't change the method signature to add a checked exception:
interface Runnable {
void run();
}
So if you do something which throws an InterruptedException
in your Runnable
implementation, and you are unable to handle it, you should set the interrupted flag on that thread, to allow calling classes to handle it:
class SleepingRunnable implements Runnable {
@Override public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
If you are able to change your method signature, it is better to do so: because InterruptedException
is a checked exception, callers are forced to handle it. This makes the fact that your thread might be interrupted much more obvious.