为什么要捕获InterruptedException来调用Thread.currentThread.interrupt()?


问题内容

在有效的Java(第275页)中,有以下代码段:

...
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();
    }
  }
}
...

捕获中断异常以重新引发它有什么用?为什么不让它飞呢?


问题答案:

简单的答案是,这InterruptedException是一个检查的异常,它不在Runnable.run方法(或Executable.execute()方法)的签名中。所以你必须抓住它。一旦发现它,Thread.interrupt()建议您设置为设置中断标志。除非您确实打算压缩中断。