AbstractQueuedLongSynchronizer.HasQueuedPredecessors 属性

定义

查询任何线程是否一直在等待获取的时间超过当前线程。

public bool HasQueuedPredecessors { [Android.Runtime.Register("hasQueuedPredecessors", "()Z", "")] get; }
[<get: Android.Runtime.Register("hasQueuedPredecessors", "()Z", "")>]
member this.HasQueuedPredecessors : bool

属性值

true 如果当前线程前面有排队线程,并且 false 当前线程位于队列的头或队列为空

属性

注解

查询任何线程是否一直在等待获取的时间超过当前线程。

此方法的调用等效于(但效率可能比):

{@code
            getFirstQueuedThread() != Thread.currentThread()
              && hasQueuedThreads()}

请注意,由于由于中断和超时而导致的取消可能随时发生,因此 true 返回不保证其他线程会在当前线程之前获取。 同样,由于队列为空,另一个线程有可能在此方法返回 false后赢得排队的争用。

此方法旨在由公平同步器使用,以避免条带。 如果此方法返回true(除非这是重新获取),则此类同步器#tryAcquire的方法应返回false#tryAcquireShared负值。 例如, tryAcquire 公平、重新进入、独占模式同步器的方法可能如下所示:

{@code
            protected boolean tryAcquire(long arg) {
              if (isHeldExclusively()) {
                // A reentrant acquire; increment hold count
                return true;
              } else if (hasQueuedPredecessors()) {
                return false;
              } else {
                // try to acquire normally
              }
            }}

已在 1.7 中添加。

Java文档java.util.concurrent.locks.AbstractQueuedLongSynchronizer.hasQueuedPredecessors()

本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。

适用于