恢复Java中的中断标志状态
再会!
我一直在浏览Java Docs&由于Sonarqube报告的错误,一些在正确处理InterruptedException
的在线资源。但是,不确定我是否100%理解中断标志
状态是否总是要恢复,每当InterruptedException发生在一个我们无法抛出Ex:Runnable实现的地方的地方。
让我们考虑以下复制的演示示例代码,其中主要方法负责启动某些异步方法。异步方法使HTTP获取请求和进程(通过将以运行> runnable
作为参数作为ADDLISTENER方法的参数)异步响应。
注意:现在我的查询是我必须还原中断状态
line#35上的标志。为什么要问这是因为,
- 这是我的完整程序,并且在任何地方都没有中断处理实际获取请求的响应的任务线程。但是,由于我了解的各种因素,可能会发生中断。但是我的要求非常简单,无论我得到什么例外,即使是
InterruptedException
,始终返回一些默认响应。因此,即使在不恢复标志的情况下,我的意图/要求也会满足IE,即通过默认响应来完成完整的future
。 - 在我的代码中,我无处可检查
thread.intrupted()
or thread.currentthread()。is Interrupted(),因为我不想将其作为我的要求通过返回默认响应来满足要求。 - 在捕获了InterruptedException并通过一些默认响应来完成未来之后,在此线程中没有进一步处理任何内容(可运行)。
- 执行InterruptedException Catch Block之后,接下来我的主线程将开始使用接收到的默认响应执行。而且我的主线程或父线程不想知道或不在乎InterruptedException是否发生过或不发生。它所关心的只是一些有效的响应。
incase如果我仍然想恢复中断标志状态,可以 有人请解释为什么还要还原,我的代码如何去 如果我不恢复它,错误?
。因为,正如我在4分中所说的那样,即使在不恢复国旗的情况下,我的要求也得到满足。
关于上述确切情况的任何启蒙/澄清都非常感谢。
提前致谢 :)
Good Day!
I have been going through the Java Docs & some online resources on properly handling the InterruptedException
due to bug reported by SonarQube. But not sure if I 100% understood if Interruption flag
status always has to be restored whenever InterruptedException occurs inside a place where we can't throw exception for Ex: Runnable implementation.
Lets consider below replicated demo example code, where the main method is responsible for initiating some async method.The async method makes the Http GET request and processes(by taking runnable
as argument to the addListener method) the response asynchronously.
Note : Now my query is Do I have to restore Interruption status
flag at line#35. Why asking this is because,
- This is my complete program, and nowhere am interrupting the task-thread which processes the actual GET request's response. But Interruption can happen due to various factors which I understand. But my requirement is very simple to always return back some default response no matter what exception I get, even if it is
InterruptedException
. So, even without restoring the flag, my intention/requirement gets fulfilled i.e to complete theCompletableFuture
with some default response. - And nowhere in my code I am going check for
Thread.interrupted()
ORThread.currentThread().isInterrupted()
because I don't want to handle this as my requirement already got fulfilled by returning back default response. - After capturing the InterruptedException and completing the Future with some default response, am not processing anything further within this thread(Runnable).
- After executing the InterruptedException catch block, next my Main Thread will start executing with the received default response. And my Main Thread OR Parent Thread doesn't want to know OR doesn't care about if InterruptedException ever occurred-or-not. All it cares about is some valid response.
Incase if I still want to restore the Interruption flag status, could
someone please explain why to restore the same, and how can my code go
wrong if I don't restore it?
. Because, as I said in above 4 points my requirement gets fulfilled even without restoring the flag.
Any enlightment/clarification on this topic for the above exact scenario is highly appreciatable.
Thanks in Advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的程序中,您可能不需要处理
InterruptedException
。但是在一般情况下,吞咽
InterruptedException
是一个坏主意。主要原因是
thread.interrupt()
(引起InterruptedException
)是中断Java标准库提供的许多阻止操作的唯一方法。例如,当我们要优雅地关闭我们的应用程序时(即,当我们要关闭文件,网络连接和其他资源之前,通常需要正确处理
InterruptedException
)。有关此信息的一些信息可以在
thread.interrupt()
javadocs 。另外,我还建议此答案以及“实践中的Java并发”中的相关章节。
In your program there you probably don't need to handle
InterruptedException
indeed.But in a general case swallowing
InterruptedException
is a bad idea.The main reason is that
Thread.interrupt()
(which causesInterruptedException
) is the only way to interrupt many blocking operations provided by Java's standard library.For instance, the proper handling of
InterruptedException
is typically required when we want to gracefully shutdown our application (i.e. when we want to close files, network connections and other resources before shutdown).Some information about this can be found in
Thread.interrupt()
javadocs.Also I would recommend this SO answer and related chapters in "Java Concurrency In Practice", mentioned there.