Class Locks

java.lang.Object
com.semedy.reasoner.core.locks.Locks

public class Locks extends Object
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 Details

    • lockAll

      public static <L extends Lock> void lockAll(Iterable<L> locks)
      Calls Lock.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 calling Lock.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
      Calls Lock.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 calling Lock.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

      public static <L extends Lock> boolean tryLockAll(Iterable<L> locks)
      Calls Lock.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 calling Lock.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
      Calls Lock.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 calling Lock.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 combined
      unit - the time unit of the time argument
      locks - 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

      public static <L extends Lock> void unlockAll(Iterable<L> locks)
      Calls Lock.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

      public static <L extends Lock> void lockAll(L... locks)
      Varargs variant of lockAll(Iterable)
      See Also:
    • lockInterruptiblyAll

      public static <L extends Lock> void lockInterruptiblyAll(L... locks) throws InterruptedException
      Throws:
      InterruptedException
      See Also:
    • tryLockAll

      public static <L extends Lock> boolean tryLockAll(L... locks)
      Varargs variant of tryLockAll(Iterable)
      See Also:
    • tryLockAll

      public static <L extends Lock> boolean tryLockAll(long time, TimeUnit unit, L... locks) throws InterruptedException
      Throws:
      InterruptedException
      See Also:
    • unlockAll

      public static <L extends Lock> void unlockAll(L... locks)
      Varargs variant of unlockAll(Iterable)
      See Also: