Class LockUtils


  • public class LockUtils
    extends Object
    • Constructor Detail

      • LockUtils

        public LockUtils()
    • Method Detail

      • repeatWithLock

        public static <T> T repeatWithLock​(int retryCount,
                                           int delayInMs,
                                           Lock lockSupplier,
                                           Callable<T> action)
        Perform an action which requires acquisition of a lock first. Repeated attempts are made to acquire the lock. If the lock cannot be acquired then the action is not run.
        Type Parameters:
        T -
        Parameters:
        retryCount -
        delayInMs -
        lockSupplier -
        action -
        Returns:
      • runWithLock

        public static <T> T runWithLock​(Lock lock,
                                        Callable<T> action)
        Perform an action which requires acquisition of a lock first. An attempt is made to acquire the lock. If this fails then the action is not run. Upon completion of the action (successful or exceptional) the lock is released again.
      • runWithMgmtLock

        public static <T,​L extends Lock> T runWithMgmtLock​(L lock,
                                                                 java.util.function.Consumer<? super L> forceUnlock,
                                                                 java.time.Duration duration,
                                                                 Callable<T> action)
        Run this action with a short-lived locked. If the lock cannot be acquired within the given time it is considered stale and forcibly unlocked. Subsequently another attempt is made to acquire the lock.