简体   繁体   English

如果线程在调用Object.wait()之前被中断,该线程甚至会在抛出InterruptedException之前费心地释放锁。

[英]If a thread is interrupted before calling Object.wait(), will the thread even bother to release the lock before throwing InterruptedException

The JDK Documentation kind of mentioned it, JDK文档中提到了它,

If the current thread is interrupted by any thread before or while it is waiting, then an > InterruptedException is thrown. 如果当前线程在等待之前或等待期间被任何线程中断,则抛出> InterruptedException。 This exception is not thrown until the lock status of this >object has been restored as described above. 如上所述,除非已恢复该对象的锁定状态,否则不会引发此异常。

I just want to be absolutely certain that the use of word "restored" means the lock must be released and reacquired, instead of being hold continuously by the thread calling Object.wait(). 我只想绝对确定使用“恢复”一词意味着必须释放并重新获得锁,而不是由调用Object.wait()的线程连续持有。 In other words, there is a chance that the lock is granted to other threads first. 换句话说,有可能首先将锁授予其他线程。

I just want to be absolutely certain that the use of word "restored" means the lock must be released and reacquired, instead of being hold continuously by the thread calling Object.wait(). 我只想绝对确定使用“恢复”一词意味着必须释放并重新获得锁,而不是由调用Object.wait()的线程连续持有。 In other words, there is a chance that the lock is granted to other threads first. 换句话说,有可能首先将锁授予其他线程。

There is never a guaranty that the lock is granted to other threads first. 永远不会保证首先将锁授予其他线程。 Even if the thread releases the lock and re-acquires it afterwards, it might succeed in re-acquiring the lock before any other thread waiting for the same lock gets a chance. 即使线程释放了锁并随后重新获得了该锁,它也可能在其他任何等待相同锁的线程获得机会之前成功地重新获得了锁。

An implementation might allow bypassing the release and re-acquire of the lock in case of an early thread interruption and it might do so even without interruption in the case of a spurious wakeup that the specification allows to occur. 一个实现可能允许在线程早期中断的情况下绕过释放和重新获取锁,即使在规范允许发生的虚假唤醒的情况下,即使没有中断也可以这样做。

In other words, when wait ends, regardless of normally or exceptionally, there is no guaranty that any other thread has been run. 换句话说,当wait结束时,无论正常或异常情况,都无法保证已运行任何其他线程。

The documentation clearly says : 文档明确指出

A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup . 线程也可以唤醒,而不会被通知,中断或超时,即所谓的虚假唤醒 While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. 尽管在实践中这种情况很少发生,但是应用程序必须通过测试应该导致线程唤醒的条件来防范它,并在条件不满足时继续等待。 In other words, waits should always occur in loops, like this one: 换句话说,等待应该总是像这样循环执行:

 synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition } 

This also hold for the case that an InterruptionException has been thrown. 对于抛出InterruptionException的情况,也是如此。 When following this general rule of how to use the intrinsic locking, your question becomes irrelevant: if one thread keeps wait ing while the condition does not hold, other threads which do not wait for this condition (or are responsible for establishing the condition) will eventually run. 当遵循有关如何使用内部锁定的一般规则时,您的问题就变得无关紧要:如果一个线程在条件不成立的情况下继续wait ,那么其他不等待该条件(或负责建立条件)的线程将最终运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM