java.util.concurrent.locks.Condition awaitUninterruptible() [英] java.util.concurrent.locks.Condition awaitUninterruptibly()

查看:70
本文介绍了java.util.concurrent.locks.Condition awaitUninterruptible()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读java.util.concurrent.locks.Condition API文档

While I am reading java.util.concurrent.locks.Condition API documentation,

我看到了:

在等待条件时,允许虚假唤醒"以 通常作为基础平台的让步而发生 语义.这对大多数应用程序几乎没有实际影响 作为条件的程序应始终循环等待, 测试正在等待的状态谓词.一个 实施是免费的,消除了虚假唤醒的可能性 但是建议应用程序程序员始终假定 它们可能会发生,因此总是在循环中等待

When waiting upon a Condition, a "spurious wakeup" is permitted to occur, in general, as a concession to the underlying platform semantics. This has little practical impact on most application programs as a Condition should always be waited upon in a loop, testing the state predicate that is being waited for. An implementation is free to remove the possibility of spurious wakeups but it is recommended that applications programmers always assume that they can occur and so always wait in a loop

和awaitUninterruptrupt()说:

and the awaitUninterruptibly() says:

如果在进入当前线程时设置了当前线程的中断状态 方法,或者它在等待时被中断,它将继续等待 直到发信号为止.当它最终从此方法返回时,其 仍然会设置中断状态

If the current thread's interrupted status is set when it enters this method, or it is interrupted while waiting, it will continue to wait until signalled. When it finally returns from this method its interrupted status will still be set

那么,这是否意味着我们不需要循环调用awaitUninterruptible()吗?请澄清一下.预先感谢.

So, does it mean that we don't need to invoke awaitUninterruptibly() in loop ? Please clarify. Thanks in advance.

推荐答案

从代码中获取:

public final void awaitUninterruptibly() {
  Node node = addConditionWaiter();
  int savedState = fullyRelease(node);
  boolean interrupted = false;
  while (!isOnSyncQueue(node)) {
    LockSupport.park(this);
    if (Thread.interrupted()) interrupted = true;
  }
  if (acquireQueued(node, savedState) || interrupted) selfInterrupt();
}

因此,等待是在循环中完成的,这样就无需在此函数之外循环.

So the waiting is done in a loop, which would remove the need to loop that outside this function.

但是请记住,这也意味着Thread.interrupt()不会执行任何操作,这可能会导致代码的某些锁定,即在关机期间.

However keep in mind that this also means that Thread.interrupt() won't do anything, which might lead to certain lock-ups of your code, i.E. during shutdown.

这篇关于java.util.concurrent.locks.Condition awaitUninterruptible()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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