Class NamedLocks<T>
- java.lang.Object
-
- org.opendaylight.infrautils.utils.concurrent.NamedLocks<T>
-
- Type Parameters:
T
- the name type, required to be effectively immutable where T.hashCode() and T.equals() is concerned
public final class NamedLocks<T> extends Object
Manages multiple ReentrantLocks identified by a unique name.Example of use of lock():
NamedLocks<String> locks; String lockName; try (Acquired lock = locks.Acquire(lockName)) { // locked region } // lock released
Example of use of tryLock():
NamedLocks<String> locks; String lockName; try (AcquireResult lock = locks.tryAcquire(lockName)) { // maybe locked region if (lock.wasAcquired()) { // locked region } else { // unlocked region } }); // lock released
- Author:
- Robert Varga
-
-
Constructor Summary
Constructors Constructor Description NamedLocks()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description NamedSimpleReentrantLock.Acquired
acquire(T lockKey)
Acquires the lock for the given key.NamedSimpleReentrantLock<T>
getLock(T lockKey)
NamedSimpleReentrantLock.AcquireResult
tryAcquire(T lockName)
Acquires the lock for the given key only if it is not held by another thread at the time of invocation.NamedSimpleReentrantLock.AcquireResult
tryAcquire(T lockName, 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.
-
-
-
Method Detail
-
tryAcquire
public NamedSimpleReentrantLock.AcquireResult tryAcquire(T lockName, 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.- Parameters:
lockName
- the key to locktimeout
- 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 failed, either due to lock time expiring or the thread being interrupted while waiting.
-
tryAcquire
public NamedSimpleReentrantLock.AcquireResult tryAcquire(T lockName)
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.- Parameters:
lockName
- the key to lock- 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 failed, because another thread is currently holding the lock.
-
acquire
public NamedSimpleReentrantLock.Acquired acquire(T lockKey)
Acquires the lock for the given key. SeeReentrantLock.lock()
for more details.- Parameters:
lockKey
- the key to lock
-
getLock
public NamedSimpleReentrantLock<T> getLock(T lockKey)
-
-