Udostępnij za pośrednictwem


ReentrantLock.HoldCount Property

Definition

Queries the number of holds on this lock by the current thread.

public virtual int HoldCount { [Android.Runtime.Register("getHoldCount", "()I", "GetGetHoldCountHandler")] get; }
[<get: Android.Runtime.Register("getHoldCount", "()I", "GetGetHoldCountHandler")>]
member this.HoldCount : int

Property Value

the number of holds on this lock by the current thread, or zero if this lock is not held by the current thread

Attributes

Remarks

Queries the number of holds on this lock by the current thread.

A thread has a hold on a lock for each lock action that is not matched by an unlock action.

The hold count information is typically only used for testing and debugging purposes. For example, if a certain section of code should not be entered with the lock already held then we can assert that fact:

{@code
            class X {
              final ReentrantLock lock = new ReentrantLock();
              // ...
              public void m() {
                assert lock.getHoldCount() == 0;
                lock.lock();
                try {
                  // ... method body
                } finally {
                  lock.unlock();
                }
              }
            }}

Java documentation for java.util.concurrent.locks.ReentrantLock.getHoldCount().

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