synchronized
methods and statements, but with extended capabilities.
More...
Inheritance diagram for ReentrantLock:
Public Member Functions | |
this (bool fair=false) | |
void | lock () |
bool | tryLock () |
bool | tryLock (long timeout, TimeUnit unit) |
void | unlock () |
Condition | newCondition () |
int | getHoldCount () |
bool | isHeldByCurrentThread () |
bool | isLocked () |
bool | isFair () |
bool | hasQueuedThreads () |
bool | hasQueuedThread (Thread thread) |
int | getQueueLength () |
bool | hasWaiters (Condition condition) |
int | getWaitQueueLength (Condition condition) |
char[] | toString () |
Protected Member Functions | |
Thread | getOwner () |
Thread[] | getQueuedThreads () |
Thread[] | getWaitingThreads (Condition condition) |
Private Attributes | |
final Sync | sync |
Classes | |
class | FairSync |
class | NonfairSync |
class | Sync |
synchronized
methods and statements, but with extended capabilities.
A ReentrantLock
is owned 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 fairness 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.
It is recommended practice to always immediately follow a call to lock
with a try
block, most typically in a before/after construction such as:
class X { private ReentrantLock lock; // ... this() { lock = new ReentrantLock; } void m() { lock.lock(); // block until condition holds try { // ... method body } finally { lock.unlock() } } }
In addition to implementing the Lock interface, this class defines methods isLocked
and getLockQueueLength
, as well as some associated protected
access methods that may be useful for instrumentation and monitoring.
This lock supports a maximum of 2147483648 recursive locks by the same thread.
Definition at line 89 of file ReentrantLock.d.
|
Creates an instance of
Definition at line 227 of file ReentrantLock.d. References sync. |
|
Acquires the lock. Acquires the lock if it is not held by another thread and returns immediately, setting the lock hold count to one. If the current thread already holds the lock then the hold count is incremented by one and the method returns immediately. If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired, at which time the lock hold count is set to one. Reimplemented from Lock. Definition at line 248 of file ReentrantLock.d. References ReentrantLock::Sync::lock(), and sync. Referenced by CyclicBarrier::dowait(), CyclicBarrier::getNumberWaiting(), CyclicBarrier::isBroken(), CyclicBarrier::reset(), and CyclicBarrier::this(). |
|
Acquires the lock only if it is not held by another thread at the time of invocation.
Acquires the lock if it is not held by another thread and returns immediately with the value
If the current thread already holds this lock then the hold count is incremented by one and the method returns
If the lock is held by another thread then this method will return immediately with the value
Reimplemented from Lock. Definition at line 275 of file ReentrantLock.d. References ReentrantLock::Sync::nonfairTryAcquire(), and sync. |
|
Acquires the lock if it is not held by another thread within the given waiting time.
Acquires the lock if it is not held by another thread and returns immediately with the value
if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
If the current thread already holds this lock then the hold count is incremented by one and the method returns If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
If the lock is acquired then the value
If the specified waiting time elapses then the value
Reimplemented from Lock. Definition at line 321 of file ReentrantLock.d. References sync, toNanos(), and AbstractLock::tryAcquireNanos(). |
|
Attempts to release this lock. If the current thread is the holder of this lock then the hold count is decremented. If the hold count is now zero then the lock is released. Reimplemented from Lock. Definition at line 332 of file ReentrantLock.d. References AbstractLock::release(), and sync. Referenced by CyclicBarrier::dowait(), CyclicBarrier::getNumberWaiting(), CyclicBarrier::isBroken(), and CyclicBarrier::reset(). |
|
Returns a Condition instance for use with this Lock instance.
Reimplemented from Lock. Definition at line 356 of file ReentrantLock.d. References ReentrantLock::Sync::newCondition(), and sync. Referenced by CyclicBarrier::this(). |
|
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:
class X { ReentrantLock lock; // ... this() { lock = new ReentrantLock; } void m() { assert( lock.getHoldCount() == 0 ); lock.lock(); try { // ... method body } finally { lock.unlock(); } } }
Definition at line 393 of file ReentrantLock.d. References ReentrantLock::Sync::getHoldCount(), and sync. |
|
Queries if this lock is held by the current thread. This method is typically used for debugging and testing.
Definition at line 404 of file ReentrantLock.d. References ReentrantLock::Sync::isHeldExclusively(), and sync. |
|
Queries if this lock is held by any thread. This method is designed for use in monitoring of the system state, not for synchronization control.
Definition at line 415 of file ReentrantLock.d. References ReentrantLock::Sync::isLocked(), and sync. |
|
Returns true if this lock has fairness set true.
Definition at line 423 of file ReentrantLock.d. References sync. |
|
Returns the thread that currently owns the exclusive lock, or Definition at line 436 of file ReentrantLock.d. References ReentrantLock::Sync::getOwner(), and sync. |
|
Queries whether any threads are waiting to acquire. Note that because cancellations may occur at any time, a 'return true if there may be other threads waiting to acquire the lock. Definition at line 450 of file ReentrantLock.d. References AbstractLock::hasQueuedThreads(), and sync. |
|
Queries whether the given thread is waiting to acquire this lock. Note that because cancellations may occur at any time, a
Definition at line 465 of file ReentrantLock.d. |
|
Returns an estimate of the number of threads waiting to acquire. The value is only an estimate because the number of threads may change dynamically while this method traverses internal data structures. This method is designed for use in monitoring of the system state, not for synchronization control.
Definition at line 479 of file ReentrantLock.d. References sync. |
|
Returns a collection containing threads that may be waiting to acquire. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive monitoring facilities.
Definition at line 493 of file ReentrantLock.d. References sync. |
|
Queries whether any threads are waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, a
Definition at line 507 of file ReentrantLock.d. References sync. |
|
Returns an estimate of the number of threads waiting on the given condition associated with this lock. Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring of the system state, not for synchronization control.
Definition at line 524 of file ReentrantLock.d. References sync. |
|
Returns a collection containing those threads that may be waiting on the given condition associated with this lock. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order. This method is designed to facilitate construction of subclasses that provide more extensive condition monitoring facilities.
Definition at line 543 of file ReentrantLock.d. References sync. |
|
Returns a string identifying this lock, as well as its lock state. The state, in brackets, includes either the String "Unlocked" or the String "Locked by" followed by the Thread.toString of the owning thread.
Definition at line 557 of file ReentrantLock.d. References ReentrantLock::Sync::getOwner(), and sync. |
|
Synchronizer providing all implementation mechanics Definition at line 92 of file ReentrantLock.d. Referenced by getHoldCount(), getOwner(), getQueuedThreads(), getQueueLength(), getWaitingThreads(), getWaitQueueLength(), hasQueuedThread(), hasQueuedThreads(), hasWaiters(), isFair(), isHeldByCurrentThread(), isLocked(), lock(), newCondition(), this(), toString(), tryLock(), and unlock(). |