Package com.seeq.utilities
Class Locks
java.lang.Object
com.seeq.utilities.Locks
A collection of helper methods for handling locks.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Used by tryWithResourcesLock.static class
Enables the synchronization of tasks between two threads in a lockstep manner, where only one thread can be executing tasks at a time. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
This method encapsulates the common need for an "if true then do, if false then lock and re-test" algorithm.static <T> T
doOrLockAndRedo
(Lock lock, Supplier<Boolean> test, T resultIfAlreadyLocked, Supplier<T> ifTrue, Supplier<T> ifFalse) static void
Locks the provided lock interruptibly, turning the InterruptedException into an OperationCanceledExceptionstatic Locks.CloseableLock
tryWithResourcesLock
(Lock lock) To be used in a try-with-resources block as such:static void
Unlocks the lock only if it not null.static void
Unlocks the lock only if it is present.
-
Method Details
-
lock
Locks the provided lock interruptibly, turning the InterruptedException into an OperationCanceledException- Parameters:
lock
- the lock to lock.
-
unlock
Unlocks the lock only if it is present.- Parameters:
lock
- The lock to unlock.
-
unlock
Unlocks the lock only if it not null.- Parameters:
lock
- The lock to unlock.
-
doOrLockAndRedo
public static <T> T doOrLockAndRedo(Lock lock, Supplier<Boolean> test, Supplier<T> ifTrue, Supplier<T> ifFalse) This method encapsulates the common need for an "if true then do, if false then lock and re-test" algorithm. Such an algorithm is common in the construction of shared resources such as singletons, where the resource should be created if it doesn't exist.- Type Parameters:
T
- The return type of this function, and thus the ifTrue and ifFalse suppliers.- Parameters:
lock
- The lock to synchronize on, if thetest
returns false.test
- If true, return the result of ifTrue without locking. If false, lock and run the test again, returning ifTrue or ifFalse respectively. This test should be side-effect-free, as it will be invoked twice on the ifFalse path.ifTrue
- Returned if test evaluates to true. Guaranteed to be invoked at most once, and only if thetest
returns true.ifFalse
- Returned if test evaluates to false. Guaranteed to be invoked at most once, and only if thetest
returns false.- Returns:
- The result of either ifTrue or ifFalse, depending on value returned by the test supplier.
-
doOrLockAndRedo
public static <T> T doOrLockAndRedo(Lock lock, Supplier<Boolean> test, @Nullable T resultIfAlreadyLocked, Supplier<T> ifTrue, Supplier<T> ifFalse) - Parameters:
resultIfAlreadyLocked
- If the lock has already been acquired, return this value instead of waiting for it. If null, then the calling thread will block on the lock.- Returns:
- The result of either ifTrue or ifFalse, depending on the result of the test predicate or resultIfAlreadyLocked if the predicate is true and the lock was already locked.
-
tryWithResourcesLock
To be used in a try-with-resources block as such:Lock lock = new ReentrantLock(); try (Locks.CloseableLock l = Locks.tryWithResourcesLock(lock)) { // Synchronized code here }
Because of the guarantees that try-with-resources provides, this pattern ensures that the lock will unlock no matter how the try block is exited.
Note: Locks interruptibly and converts the InterruptedException to an OperationCanceledException.
- Parameters:
lock
- Lock to lock and unlock as part of the try-with-resources block.- Returns:
- Should be used only by the try-with-resources internals, and should not be used directly.
-