다음을 통해 공유


ReentrantLock Class

Definition

A reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities.

[Android.Runtime.Register("java/util/concurrent/locks/ReentrantLock", DoNotGenerateAcw=true)]
public class ReentrantLock : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ISerializable, Java.Util.Concurrent.Locks.ILock
[<Android.Runtime.Register("java/util/concurrent/locks/ReentrantLock", DoNotGenerateAcw=true)>]
type ReentrantLock = class
    inherit Object
    interface ISerializable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
    interface ILock
Inheritance
ReentrantLock
Attributes
Implements

Remarks

A reentrant mutual exclusion Lock with the same basic behavior and semantics as the implicit monitor lock accessed using synchronized methods and statements, but with extended capabilities.

A ReentrantLock is <em>owned</em> by the thread last successfully locking, but not yet unlocking it. A thread invoking lock will return, successfully acquiring the lock, when the lock is not owned by another thread. The method will return immediately if the current thread already owns the lock. This can be checked using methods #isHeldByCurrentThread, and #getHoldCount.

The constructor for this class accepts an optional <em>fairness</em> parameter. When set true, under contention, locks favor granting access to the longest-waiting thread. Otherwise this lock does not guarantee any particular access order. Programs using fair locks accessed by many threads may display lower overall throughput (i.e., are slower; often much slower) than those using the default setting, but have smaller variances in times to obtain locks and guarantee lack of starvation. Note however, that fairness of locks does not guarantee fairness of thread scheduling. Thus, one of many threads using a fair lock may obtain it multiple times in succession while other active threads are not progressing and not currently holding the lock. Also note that the untimed #tryLock() method does not honor the fairness setting. It will succeed if the lock is available even if other threads are waiting.

It is recommended practice to <em>always</em> immediately follow a call to lock with a try block, most typically in a before/after construction such as:

{@code
            class X {
              private final ReentrantLock lock = new ReentrantLock();
              // ...

              public void m() {
                lock.lock();  // block until condition holds
                try {
                  // ... method body
                } finally {
                  lock.unlock();
                }
              }
            }}

In addition to implementing the Lock interface, this class defines a number of public and protected methods for inspecting the state of the lock. Some of these methods are only useful for instrumentation and monitoring.

Serialization of this class behaves in the same way as built-in locks: a deserialized lock is in the unlocked state, regardless of its state when serialized.

This lock supports a maximum of 2147483647 recursive locks by the same thread. Attempts to exceed this limit result in Error throws from locking methods.

Added in 1.5.

Java documentation for java.util.concurrent.locks.ReentrantLock.

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.

Constructors

ReentrantLock()

Creates an instance of ReentrantLock.

ReentrantLock(Boolean)

Creates an instance of ReentrantLock with the given fairness policy.

ReentrantLock(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
HasQueuedThreads

Queries whether any threads are waiting to acquire this lock.

HoldCount

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

IsFair

Returns true if this lock has fairness set true.

IsHeldByCurrentThread

Queries if this lock is held by the current thread.

IsLocked

Queries if this lock is held by any thread.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Owner

Returns the thread that currently owns this lock, or null if not owned.

PeerReference (Inherited from Object)
QueuedThreads

Returns a collection containing threads that may be waiting to acquire this lock.

QueueLength

Returns an estimate of the number of threads waiting to acquire this lock.

ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
GetWaitingThreads(ICondition)

Returns a collection containing those threads that may be waiting on the given condition associated with this lock.

GetWaitQueueLength(ICondition)

Returns an estimate of the number of threads waiting on the given condition associated with this lock.

HasQueuedThread(Thread)

Queries whether the given thread is waiting to acquire this lock.

HasWaiters(ICondition)

Queries whether any threads are waiting on the given condition associated with this lock.

JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Lock()

Acquires the lock.

LockInterruptibly()

Acquires the lock unless the current thread is Thread#interrupt interrupted.

NewCondition()

Returns a Condition instance for use with this Lock instance.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
TryLock()

Acquires the lock only if it is not held by another thread at the time of invocation.

TryLock(Int64, TimeUnit)

Acquires the lock if it is not held by another thread within the given waiting time and the current thread has not been Thread#interrupt interrupted.

Unlock()

Attempts to release this lock.

UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to