In Effective Java (page 275), there is this code segment:
在有效的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();
}
}
}
...
What's the use of catching the interrupted exception just to re-raise it? Why not just let it fly?
捕获被中断的异常以重新引发异常有什么用?为什么不让它飞起来呢?
3 个解决方案
#1
15
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.
简单的答案是InterruptedException是一个已检查的异常,它不在runnabler .run方法(或Executable.execute()方法)的签名中。所以你必须抓住它。一旦您捕获了它,就应该调用Thread.interrupt()来设置中断标志……除非你真的想中断。
#2
0
Sometimes you can't ignore exception and you must catch it. Mainly this happens when you override method which can't throw InterruptedException
in accordance with its signature. For example, this approach is usually used in Runnable.run()
method.
有时你不能忽视异常,你必须抓住它。这主要发生在重写方法时,该方法不能根据其签名抛出InterruptedException。例如,这种方法通常在runnabler .run()方法中使用。
#3
0
The executor can interrupt tasks if they are cancelled but it clears the interrupted flag between tasks to avoid one cancelled task interrupting an unrelated task.
如果任务被取消,执行程序可以中断任务,但它会清除任务之间的中断标志,以避免一个被取消的任务中断不相关的任务。
As such, interrupting the current thread here would be dangerous if it actually did anything.
因此,如果中断当前线程实际上做了任何事情,那么它将是危险的。
A simpler way around this is to use Callable or ignore the interrupt.
更简单的方法是使用可调用或忽略中断。
Additionally it is a good idea to catch and log any error or exception thrown in the try/catch block otherwise the exception/error will be discarded and your program could be failing but you won't know it is or why.
此外,捕获和记录在try/catch块中抛出的任何错误或异常都是一个好主意,否则将丢弃异常/错误,您的程序可能会失败,但您不会知道它是什么或为什么。
#1
15
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.
简单的答案是InterruptedException是一个已检查的异常,它不在runnabler .run方法(或Executable.execute()方法)的签名中。所以你必须抓住它。一旦您捕获了它,就应该调用Thread.interrupt()来设置中断标志……除非你真的想中断。
#2
0
Sometimes you can't ignore exception and you must catch it. Mainly this happens when you override method which can't throw InterruptedException
in accordance with its signature. For example, this approach is usually used in Runnable.run()
method.
有时你不能忽视异常,你必须抓住它。这主要发生在重写方法时,该方法不能根据其签名抛出InterruptedException。例如,这种方法通常在runnabler .run()方法中使用。
#3
0
The executor can interrupt tasks if they are cancelled but it clears the interrupted flag between tasks to avoid one cancelled task interrupting an unrelated task.
如果任务被取消,执行程序可以中断任务,但它会清除任务之间的中断标志,以避免一个被取消的任务中断不相关的任务。
As such, interrupting the current thread here would be dangerous if it actually did anything.
因此,如果中断当前线程实际上做了任何事情,那么它将是危险的。
A simpler way around this is to use Callable or ignore the interrupt.
更简单的方法是使用可调用或忽略中断。
Additionally it is a good idea to catch and log any error or exception thrown in the try/catch block otherwise the exception/error will be discarded and your program could be failing but you won't know it is or why.
此外,捕获和记录在try/catch块中抛出的任何错误或异常都是一个好主意,否则将丢弃异常/错误,您的程序可能会失败,但您不会知道它是什么或为什么。