Class Locks

java.lang.Object
com.seeq.utilities.Locks

@ParametersAreNonnullByDefault public final class Locks extends Object
A collection of helper methods for handling locks.
  • Method Details

    • lock

      public static void lock(Lock lock)
      Locks the provided lock interruptibly, turning the InterruptedException into an OperationCanceledException
      Parameters:
      lock - the lock to lock.
    • unlock

      public static void unlock(Optional<Lock> lock)
      Unlocks the lock only if it is present.
      Parameters:
      lock - The lock to unlock.
    • unlock

      public static void unlock(@Nullable Lock lock)
      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 the test 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 the test returns true.
      ifFalse - Returned if test evaluates to false. Guaranteed to be invoked at most once, and only if the test 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

      public static Locks.CloseableLock tryWithResourcesLock(Lock lock)
      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.