恢复Java中的中断标志状态

发布于 2025-01-24 22:11:28 字数 1325 浏览 4 评论 0原文

再会!

我一直在浏览Java Docs&由于Sonarqube报告的错误,一些在正确处理InterruptedException的在线资源。但是,不确定我是否100%理解中断标志状态是否总是要恢复,每当InterruptedException发生在一个我们无法抛出Ex:Runnable实现的地方的地方。

让我们考虑以下复制的演示示例代码,其中主要方法负责启动某些异步方法。异步方法使HTTP获取请求和进程(通过将运行> runnable作为参数作为ADDLISTENER方法的参数)异步响应。

注意:现在我的查询是我必须还原中断状态 line#35上的标志。为什么要问这是因为,

  1. 这是我的完整程序,并且在任何地方都没有中断处理实际获取请求的响应的任务线程。但是,由于我了解的各种因素,可能会发生中断。但是我的要求非常简单,无论我得到什么例外,即使是InterruptedException,始终返回一些默认响应。因此,即使在不恢复标志的情况下,我的意图/要求也会满足IE,即通过默认响应来完成完整的future
  2. 在我的代码中,我无处可检查thread.intrupted() or thread.currentthread()。is Interrupted(),因为我不想将其作为我的要求通过返回默认响应来满足要求。
  3. 在捕获了InterruptedException并通过一些默认响应来完成未来之后,在此线程中没有进一步处理任何内容(可运行)。
  4. 执行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.
enter image description here

Note : Now my query is Do I have to restore Interruption status flag at line#35. Why asking this is because,

  1. 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 the CompletableFuture with some default response.
  2. And nowhere in my code I am going check for Thread.interrupted() OR Thread.currentThread().isInterrupted() because I don't want to handle this as my requirement already got fulfilled by returning back default response.
  3. After capturing the InterruptedException and completing the Future with some default response, am not processing anything further within this thread(Runnable).
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2025-01-31 22:11:28

在您的程序中,您可能不需要处理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 causes InterruptedException) 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文