Class NamedSimpleReentrantLock<T>
- java.lang.Object
-
- java.util.concurrent.locks.ReentrantLock
-
- org.opendaylight.infrautils.utils.concurrent.NamedSimpleReentrantLock<T>
-
- Type Parameters:
T- the name type, required to be effectively immutable where T.hashCode() and T.equals() is concerned
- All Implemented Interfaces:
Serializable,Lock
public final class NamedSimpleReentrantLock<T> extends ReentrantLock
AReentrantLockwhich has a name and provides simplified locking operations.- Author:
- Robert Varga
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classNamedSimpleReentrantLock.AcquiredResult of a successful lock operation, representing a single lease on the lock.static classNamedSimpleReentrantLock.AcquireResultBase result of a locking operation.
-
Constructor Summary
Constructors Constructor Description NamedSimpleReentrantLock(T name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NamedSimpleReentrantLock.Acquiredacquire()Acquires the lock.TgetName()StringtoString()NamedSimpleReentrantLock.AcquireResulttryAcquire()Acquires the lock for the given key only if it is not held by another thread at the time of invocation.NamedSimpleReentrantLock.AcquireResulttryAcquire(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.-
Methods inherited from class java.util.concurrent.locks.ReentrantLock
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, lock, lockInterruptibly, newCondition, tryLock, tryLock, unlock
-
-
-
-
Constructor Detail
-
NamedSimpleReentrantLock
public NamedSimpleReentrantLock(T name)
-
-
Method Detail
-
getName
public T getName()
-
tryAcquire
public 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. SeeReentrantLock.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- Parameters:
timeout- the time to wait for the lockunit- the time unit of the timeout argument- Returns:
- lock operation result. If the lock was free and was acquired by the current thread,
NamedSimpleReentrantLock.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.
-
tryAcquire
public NamedSimpleReentrantLock.AcquireResult tryAcquire()
Acquires the lock for the given key only if it is not held by another thread at the time of invocation. SeeReentrantLock.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- Returns:
- lock operation result. If the lock was free and was acquired by the current thread,
NamedSimpleReentrantLock.AcquireResult.wasAcquired()will return true. If it reports false, the locking attempt has failed,because another thread is currently holding the lock.
-
acquire
public NamedSimpleReentrantLock.Acquired acquire()
Acquires the lock. SeeReentrantLock.lock()for more details. Example of use:NamedSimpleReentrantLocks<?> lock; try (Acquired acq = lock.acquire()) { // locked region } // lock released
-
toString
public String toString()
- Overrides:
toStringin classReentrantLock
-
-