Class Locks
java.lang.Object
com.semedy.reasoner.utils.upgradeablelock.Locks
Utility methods to group-lock and group-unlock collections of locks, including roll back support to
ensure that either all locks are acquired or no locks are acquired.
- Author:
- Niall Gallagher
-
Method Summary
Modifier and TypeMethodDescriptionstatic <L extends Lock>
voidCallsLock.lock()
on all locks provided by the given iterable, in the order provided by the iterable.static <L extends Lock>
voidlockAll
(L... locks) Varargs variant oflockAll(Iterable)
static <L extends Lock>
voidlockInterruptiblyAll
(Iterable<L> locks) CallsLock.lockInterruptibly()
on all locks provided by the given iterable, in the order provided by the iterable.static <L extends Lock>
voidlockInterruptiblyAll
(L... locks) Varargs variant oflockInterruptiblyAll(Iterable)
static <L extends Lock>
booleantryLockAll
(long time, TimeUnit unit, Iterable<L> locks) CallsLock.tryLock()
on all locks provided by the given iterable, in the order provided by the iterable.static <L extends Lock>
booleantryLockAll
(long time, TimeUnit unit, L... locks) Varargs variant oftryLockAll(long, java.util.concurrent.TimeUnit, Iterable)
static <L extends Lock>
booleantryLockAll
(Iterable<L> locks) CallsLock.tryLock()
on all locks provided by the given iterable, in the order provided by the iterable.static <L extends Lock>
booleantryLockAll
(L... locks) Varargs variant oftryLockAll(Iterable)
static <L extends Lock>
voidCallsLock.unlock()
on all locks provided by the given iterable, in the order provided by the iterable.static <L extends Lock>
voidunlockAll
(L... locks)
-
Method Details
-
lockAll
CallsLock.lock()
on all locks provided by the given iterable, in the order provided by the iterable. Automatically releases any locks acquired (in reverse order) by callingLock.unlock()
if an exception is thrown, before re-throwing the exception.- Type Parameters:
L
- Type of the lock- Parameters:
locks
- The locks to acquire
-
lockInterruptiblyAll
public static <L extends Lock> void lockInterruptiblyAll(Iterable<L> locks) throws InterruptedException CallsLock.lockInterruptibly()
on all locks provided by the given iterable, in the order provided by the iterable. Automatically releases any locks acquired (in reverse order) by callingLock.unlock()
if the thread is interrupted while waiting for a lock or if an exception is thrown, before re-throwing the exception.- Type Parameters:
L
- Type of the lock- Parameters:
locks
- The locks to acquire- Throws:
InterruptedException
- If the thread is interrupted while waiting for a lock
-
tryLockAll
CallsLock.tryLock()
on all locks provided by the given iterable, in the order provided by the iterable. Automatically releases any locks acquired (in reverse order) by callingLock.unlock()
if it is not possible to obtain any lock, if the thread is interrupted while waiting for a lock, or if an exception is thrown, before re-throwing the exception.- Type Parameters:
L
- Type of the lock- Parameters:
locks
- The locks to acquire- Returns:
- True if at least one lock was supplied and all supplied locks were acquired successfully, otherwise false
-
tryLockAll
public static <L extends Lock> boolean tryLockAll(long time, TimeUnit unit, Iterable<L> locks) throws InterruptedException CallsLock.tryLock()
on all locks provided by the given iterable, in the order provided by the iterable. Automatically releases any locks acquired (in reverse order) by callingLock.unlock()
if it is not possible to obtain any lock within the remaining time within the timeout given, if the thread is interrupted while waiting for a lock, or if an exception is thrown, before re-throwing the exception.- Type Parameters:
L
- Type of the lock- Parameters:
time
- the maximum time to wait for all locks combinedunit
- the time unit of thetime
argumentlocks
- The locks to acquire- Returns:
- True if at least one lock was supplied and all supplied locks were acquired successfully, otherwise false
- Throws:
InterruptedException
- If the thread is interrupted while waiting for a lock
-
unlockAll
CallsLock.unlock()
on all locks provided by the given iterable, in the order provided by the iterable. Note you may therefore wish to supply locks in reverse order.- Type Parameters:
L
- Type of the lock- Parameters:
locks
- The locks to unlock
-
lockAll
Varargs variant oflockAll(Iterable)
- See Also:
-
lockInterruptiblyAll
@SafeVarargs public static <L extends Lock> void lockInterruptiblyAll(L... locks) throws InterruptedException Varargs variant oflockInterruptiblyAll(Iterable)
- Throws:
InterruptedException
- See Also:
-
tryLockAll
Varargs variant oftryLockAll(Iterable)
- See Also:
-
tryLockAll
@SafeVarargs public static <L extends Lock> boolean tryLockAll(long time, TimeUnit unit, L... locks) throws InterruptedException Varargs variant oftryLockAll(long, java.util.concurrent.TimeUnit, Iterable)
-
unlockAll
-