java - 什么样的行为会导致异常中断?

标签 java multithreading exception

我对 Java 中的线程比较陌生,我注意到每次使用 Thread.sleep() 时,我都必须捕获 InterrupetdException。

什么样的行为会导致这种情况,在我有监控线程的简单应用程序中,我可以忽略异常吗?

最佳答案

当有人调用 interrupt() 时会发生这种情况在线程上。 This article by Brian Goetz解释了中断机制以及应该如何处理 InterruptedExceptions:

"The most common response to InterruptedException is to swallow it -- catch it and do nothing (or perhaps log it, which isn't any better) -- as we'll see later in Listing 4. Unfortunately, this approach throws away important information about the fact that an interrupt occurred, which could compromise the application's ability to cancel activities or shut down in a timely manner."

"If you catch InterruptedException but cannot rethrow it, you should preserve evidence that the interruption occurred [...]. This task is accomplished by calling interrupt() to "reinterrupt" the current thread."

关于java - 什么样的行为会导致异常中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/225194/

相关文章:

java - owasp 自动代码审查工具

java - 使用泛型参数重写方法

java - 将后端绑定(bind)到 gui java

java - 在Java并发教程的fork/join部分中, "computeDirectly"函数是如何通过 "invokeAll"执行的?

java - 将递增值插入 ArrayBlockingQueue 和线程安全

java - Google Guice 3 有什么新功能?

c - C 中 POSIX 线程运行时间和属性显示

ios - 在没有突变的情况下得到 "NSOrderedSetArrayProxy was mutated while being enumerated"错误

c# - 尝试插入带有引号的数据时,sqlite 插入查询中出现语法错误

c# - Ninject - Binding.GetProvider 抛出 NullReferenceException