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 InterruptedException
is a checked exception and it is not in the signature of the Runnable.run
method (or the Executable.execute()
method). So you have to catch it. And once you've caught it, calling Thread.interrupt()
to set the interrupted flag is the recommended thing to do ... unless you really intend to squash the interrupt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With