In Effective Java (page 275), there is this code segment:
...
for (int i = 0; i < concurrency; i++) {
executor.execute(new Runnable() {
public void run() {
ready.countDown();
try {
start.await();
action.run();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
done.countDown();
}
}
}
...
What’s the use of catching the interrupted exception just to re-raise it? Why not just let it fly?
The simple answer is that
InterruptedExceptionis a checked exception and it is not in the signature of theRunnable.runmethod (or theExecutable.execute()method). So you have to catch it. And once you’ve caught it, callingThread.interrupt()to set the interrupted flag is the recommended thing to do … unless you really intend to squash the interrupt.