为什么Thread.sleep()清除中断的标志? [英] WHY does Thread.sleep() clear the interrupted flag?

查看:135
本文介绍了为什么Thread.sleep()清除中断的标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道某些Thread方法会清除中断的标志(例如,sleep,wait等). 但是为什么他们要这样做呢?是什么原因呢?

I know that some Thread methods clear the interrupted flag (e.g. sleep, wait, ...). But why do they do this? What is the reason for it?

推荐答案

这是由于中断被设计为 not 与取消完全同义的结果. Oracle的指导原则是仅将中断用于取消,但该视图可能是随着时间推移而到达的.无论如何,设计都不会强迫这样做.您可以设计一个任务来响应中断,然后返回到正在执行的操作.

That is a result of interruption being designed not to be totally synonymous with cancellation. The guidance from Oracle is to use interruption only for cancellation, but that view may have been arrived at over time. In any event the design doesn't force that. You can design a task to respond to interruption, then go back to what it was doing.

在Java Concurrency in Practice,7.1,第138页中,它说:

In Java Concurrency in Practice, 7.1, page 138, it says:

API或语言规范中没有任何内容可以将中断与任何特定的取消语义相关联,但是在实践中,对除中断之外的任何事物使用中断都是脆弱且难以在较大的应用程序中维持的.

There is nothing in the API or language specification that ties interruption to any specific cancellation semantics, but in practice, using interruption for anything but cancellation is fragile and dfficult to sustain in larger applications.

它的设计方式在任何给定时间都只有一件事,就是捕获InterruptedException或中断标志,以告诉线程状态是什么.如果正在进行InterruptedException,则标志的值无关紧要.而且想法似乎是,捕获异常的代码应该决定是否设置中断标志.

The way it was designed there is exactly one thing at any given time, either the InterruptedException being caught or the interrupt flag, to tell what the status of the thread is. If the InterruptedException is in-flight the value of the flag is irrelevant. And the idea seems to have been that the code catching the exception should decide whether the interrupted flag should be set or not.

这样,使用InterruptedException退出循环很方便:

This way it's convenient to use the InterruptedException to exit a loop:

try {
    while (!Thread.currentThread().isInterrupted()) {
        Thread.sleep(5000);
        // ... do stuff ...
    }
} catch (InterruptedException e) {
    Thread.currentThread().interrupt();
}

由于异常是在循环之外捕获的,因此线程停止循环,这与周围的代码无关,无论是否设置了中断标志.但是,如果您在循环中捕获了InterruptedException,例如:

Here the thread stops looping since the exception is caught outside the loop, it only matters to any surrounding code whether the interrupt flag is set or not. However, if you catch the InterruptedException within the loop, like:

while (!Thread.currentThread().isInterrupted()) {
    // ... do stuff ...
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
}

(很多代码都这样做了,因为程序员已经把它搞砸了,他们甚至永远也不想考虑使用异常进行流控制),然后必须设置中断标志.

(and a lot of code does this, because programmers have had it pounded into their heads that they should never ever even think about using exceptions for flow control), then you have to set the interrupted flag.

我认为目的是应用程序开发人员应使用该异常来逃避中断的上下文,在源头附近吃掉该异常无法正常工作.

I think the intention is that the application developer should use the exception to escape the context of the interruption, eating the exception close to the source does not work as cleanly.

这篇关于为什么Thread.sleep()清除中断的标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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