Share via


AbstractQueuedSynchronizer.HasQueuedPredecessors Property

Definition

Queries whether any threads have been waiting to acquire longer than the current thread.

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

Property Value

true if there is a queued thread preceding the current thread, and false if the current thread is at the head of the queue or the queue is empty

Attributes

Remarks

Queries whether any threads have been waiting to acquire longer than the current thread.

An invocation of this method is equivalent to (but may be more efficient than):

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

Note that because cancellations due to interrupts and timeouts may occur at any time, a true return does not guarantee that some other thread will acquire before the current thread. Likewise, it is possible for another thread to win a race to enqueue after this method has returned false, due to the queue being empty.

This method is designed to be used by a fair synchronizer to avoid barging. Such a synchronizer's #tryAcquire method should return false, and its #tryAcquireShared method should return a negative value, if this method returns true (unless this is a reentrant acquire). For example, the tryAcquire method for a fair, reentrant, exclusive mode synchronizer might look like this:

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

Added in 1.7.

Java documentation for java.util.concurrent.locks.AbstractQueuedSynchronizer.hasQueuedPredecessors().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to