CountDownLatch是否受到虚假唤醒的影响? [英] Is CountDownLatch affected by spurious wakeups?

查看:221
本文介绍了CountDownLatch是否受到虚假唤醒的影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

诸如等待/通知和锁定/条件之类的并发管理机制似乎受到虚假唤醒的影响.开发人员通过重新检查条件确实已发生变化来迎合那些意外的唤醒.

Concurrency management mechanisms such as wait/notify and lock/condition seem to be affected by spurious wakeups. Developers cater for those unexpected wakeups by re-checking that the condition has indeed changed.

当涉及到CountDownLatch时,虚假唤醒是否是一个问题?

When it comes to CountDownLatch, are spurious wakeups an issue?

推荐答案

如果当前计数大于零,则当前线程 出于线程调度目的而被禁用,并且处于休眠状态,直到 发生两件事之一:

If the current count is greater than zero then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happen:

  • 由于调用countDown()方法而使计数达到零;或
  • 其他一些线程中断了当前线程.
  • The count reaches zero due to invocations of the countDown() method; or
  • Some other thread interrupts the current thread.

休眠表示该方法不会返回.换句话说,尽管会发生虚假的唤醒,但这不会导致await方法返回.

dormant meaning the method won't return. In other words, though a spurious wakeup can occur, it won't cause the await method to return.

您可以查看LockSuport#park或Object#wait受伪造唤醒的影响),直到满足条件为止.

You can look at the implementation to see how this is done exactly, but, in brief, it's the typical trick of looping and "waiting" (through LockSuport#park or Object#wait which are affected by spurious wakeups) until a condition is met.

当涉及到CountDownLatch时,虚假唤醒是否是一个问题?

When it comes to CountDownLatch, are spurious wakeups an issue?

否.

这篇关于CountDownLatch是否受到虚假唤醒的影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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