InterruptedException:是什么原因造成的? [英] InterruptedException : what causes it?

查看:856
本文介绍了InterruptedException:是什么原因造成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Java的 InterruptedException有一些有趣的问题和答案,例如 InterruptedException的原因在Java中处理InterruptedException 。但是,它们都没有告诉我InterruptedException的可能来源。

There are interesting questions and answers regarding Java's InterruptedException, for example The Cause of InterruptedException and Handling InterruptedException in Java. However, none of them tells me about the possible sources of InterruptedException.

如SIGTERM,SIGQUIT,SIGINT等OS信号怎么样?在命令行上按CTRL-C会产生InterruptedException吗?还有什么?

What about OS signals like SIGTERM, SIGQUIT, SIGINT? Does pressing CTRL-C on the command line produce an InterruptedException? What else?

推荐答案

你列出的所有内容都不会产生 InterruptedException

None of the things you list produce an InterruptedException.

唯一可以中断线程的是对 线程#中断() 。 JLS在此问题上相对明确,来自第17.2节.3

The only thing that can interrupt a thread is a call to Thread#interrupt(). The JLS is relatively clear on the matter, from section 17.2.3:


17.2.3中断

调用 Thread.interrupt 时会发生中断操作,以及定义为依次调用它的
方法,例如 ThreadGroup.interrupt

Interruption actions occur upon invocation of Thread.interrupt, as well as methods defined to invoke it in turn, such as ThreadGroup.interrupt.

参见关于中断的官方教程以获取更多信息。具体来说:

See the official tutorial on interrupts for some more info as well. Specifically:


线程通过在<$ c上调用 interrupt 发送中断$ c> Thread 要中断的线程的对象。为了使中断机制正常工作,被中断的线程必须支持自己的中断。

A thread sends an interrupt by invoking interrupt on the Thread object for the thread to be interrupted. For the interrupt mechanism to work correctly, the interrupted thread must support its own interruption.

...

中断机制使用称为中断状态的内部标志实现。调用 Thread.interrupt 设置此标志。当线程通过调用静态方法 Thread.interrupted 来检查中断时,将清除中断状态。非静态 isInterrupted 方法,一个线程用来查询另一个线程的中断状态,不会改变中断状态标志。

The interrupt mechanism is implemented using an internal flag known as the interrupt status. Invoking Thread.interrupt sets this flag. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.

按照惯例,任何通过抛出 InterruptedException 退出的方法都会在执行此操作时清除中断状态。但是,通过另一个调用 interrupt 的线程,可以立即再次设置中断状态。

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

这意味着它只能通过调用 interrupt()来设置显式标志,而不是由其他未知的外部事件触发。抛出它的各种方法中的异常描述进一步暗示了这一点,例如(强调我的):

The implication is that it is an explicit flag settable only by calling interrupt(), rather than triggered by other unknown external events. This is further implied by the description of the exception in various methods that throw it, for example (emphasis mine):


InterruptedException - 如果有任何线程中断当前线程。抛出此异常时,将清除当前线程的中断状态。

InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.






中断系统的目的通常是提供一个通用的,定义良好的框架,允许线程在其他线程中中断任务(可能是耗时的)。虽然您可以在自己的应用程序中使用显式逻辑实现类似的功能,但是使用这种定义良好的机制允许独立的类(例如JDK,其他第三方代码,您自己的代码中的其他独立类)以一致的方式提供此功能。 。


The purpose of the interrupt system in general is to provide a common, well-defined framework for allowing threads to interrupt tasks (potentially time-consuming ones) in other threads. While you could implement similar functionality with explicit logic in your own application, having this well-defined mechanism allows independent classes (e.g. the JDK, other third-party code, other independent classes in your own code) to provide this functionality in a consistent way.

你看到的关于处理 InterruptedException 的许多注释和警告并不意味着他们可以完全自发抛出,它们是为了鼓励设计良好的对象,可以在尚未知的情境中使用,其中 interrupt()将被认为是有效的(所以实际上,你想要假设如果你正在创建在以后的情况下会很健壮的可重用对象,它们会被自发抛出 - 也就是说,你永远不能保证你的代码不会在某一天被希望中断工作的人。)

The many notes and "warnings" you see about handling InterruptedException aren't meant to imply that they can be thrown completely spontaneously, they are meant to encourage well-designed objects that can be used in contexts that are not known yet, where interrupt() would be presumed to work (so really, you do want to assume they can be thrown spontaneously if you are creating reusable objects that will be robust in future situations - i.e. you're never guaranteed that your code won't be some day used by somebody who expects interrupts to work).

对于快速的一次性项目,你不真的需要担心特殊处理这些异常,只要你确定你没有调用 interrupt()并且没有调用可以调用中断的东西( ),但从长远来看,请注意这种影响,特别是如果你最终在其他环境中重复使用该代码。

For quick one-off projects you don't really need to worry about special handling for these exceptions as long as you know for certain that you aren't calling interrupt() and aren't calling something that can call interrupt(), but be aware of the implications of that in the long run, especially if you end up reusing that code in other contexts.

这篇关于InterruptedException:是什么原因造成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆