T
- the name type, required to be effectively immutable where T.hashCode() and T.equals() is concernedpublic final class NamedSimpleReentrantLock<T> extends ReentrantLock
ReentrantLock
which has a name and provides simplified locking operations.Modifier and Type | Class and Description |
---|---|
static class |
NamedSimpleReentrantLock.Acquired
Result of a successful lock operation, representing a single lease on the lock.
|
static class |
NamedSimpleReentrantLock.AcquireResult
Base result of a locking operation.
|
Constructor and Description |
---|
NamedSimpleReentrantLock(T name) |
Modifier and Type | Method and Description |
---|---|
NamedSimpleReentrantLock.Acquired |
acquire()
Acquires the lock.
|
T |
getName() |
String |
toString() |
NamedSimpleReentrantLock.AcquireResult |
tryAcquire()
Acquires the lock for the given key only if it is not held by another thread at the time of invocation.
|
NamedSimpleReentrantLock.AcquireResult |
tryAcquire(long timeout,
TimeUnit unit)
Tries to acquire the lock for the given key if it is not held by another thread within the given waiting time.
|
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, lock, lockInterruptibly, newCondition, tryLock, tryLock, unlock
public NamedSimpleReentrantLock(T name)
public T getName()
public NamedSimpleReentrantLock.AcquireResult tryAcquire(long timeout, TimeUnit unit)
ReentrantLock.tryLock(long, TimeUnit)
for more details.
Example of use::
NamedSimpleReentrantLockslt;?> lock; try (AcquireResult acq = lock.tryAcquire(1, TimeUnit.SECONDS)) { // maybe locked region, not safe if (acq.wasAcquired()) { // locked region } else { // unlocked region } }); // lock released
timeout
- the time to wait for the lockunit
- the time unit of the timeout argumentNamedSimpleReentrantLock.AcquireResult.wasAcquired()
will return true. If it reports false, the locking attempt has
failed, either due to lock time expiring or the thread being interrupted while waiting.public NamedSimpleReentrantLock.AcquireResult tryAcquire()
ReentrantLock.tryLock()
for more details.
Example of use::
NamedSimpleReentrantLock<?> lock; try (AcquireResult acq = locks.tryAcquire()) { // maybe locked region if (acq.wasAcquired()) { // locked region } else { // unlocked region } }); // lock released
NamedSimpleReentrantLock.AcquireResult.wasAcquired()
will return true. If it reports false, the locking attempt has
failed,because another thread is currently holding the lock.public NamedSimpleReentrantLock.Acquired acquire()
ReentrantLock.lock()
for more details. Example of use:
NamedSimpleReentrantLocks<?> lock; try (Acquired acq = lock.acquire()) { // locked region } // lock released
public String toString()
toString
in class ReentrantLock
Copyright © 2019 OpenDaylight. All rights reserved.