gpt4 book ai didi

java - 如何处理 InterruptedException

转载 作者:行者123 更新时间:2023-11-30 04:45:34 27 4
gpt4 key购买 nike

Possible Duplicate:
Handling InterruptedException in Java

我想知道如何InterruptedException应妥善处理。 catch block 中应该执行哪些操作?是否有任何情况应该将异常委托(delegate)给更高级别的类?

最佳答案

InterruptedException 的要点是允许阻塞方法在请求时提前取消。你不应该做的一件事就是什么都不做;不要只是吞掉异常。

如果您无法从方法中抛出异常,那么调用 Thread.currentThread().interrupt() 通常是一个不错的选择。

查看 Brian Goetz 的文章 http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html ,对这个话题进行了很好的讨论。编辑:看起来有人已经推荐了这篇文章 - 无论如何,这是一篇很好的读物。

关于java - 如何处理 InterruptedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11023647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com