Class SimpleLock

java.lang.Object
java.util.concurrent.locks.ReentrantLock
com.helger.commons.concurrent.SimpleLock
All Implemented Interfaces:
Serializable, Lock

public class SimpleLock extends ReentrantLock
This is an extension around ReentrantLock that allows for easy usage with Java 8 :) See locked(Runnable) and locked(Runnable) method. Also methods for suppliers are available.
Author:
Philip Helger
See Also:
  • Constructor Details

    • SimpleLock

      public SimpleLock()
      Default constructor creating a default ReentrantLock
    • SimpleLock

      public SimpleLock(boolean bFair)
      Constructor creating a ReentrantLock with the provided fairness
      Parameters:
      bFair - true if this lock should use a fair ordering policy
  • Method Details

    • locked

      public void locked(@Nonnull Runnable aRunnable)
      Execute the provided runnable in a read lock.
      Parameters:
      aRunnable - Runnable to be executed. May not be null.
    • lockedThrowing

      public <EXTYPE extends Exception> void lockedThrowing(@Nonnull IThrowingRunnable<EXTYPE> aRunnable) throws EXTYPE
      Execute the provided runnable in a read lock.
      Type Parameters:
      EXTYPE - Exception type to be thrown
      Parameters:
      aRunnable - Runnable to be executed. May not be null.
      Throws:
      EXTYPE - If the callable throws the exception
    • lockedGet

      public <T> T lockedGet(@Nonnull Supplier<? extends T> aSupplier)
      Execute the provided callable in a read lock. Note: no nullable/non-nullable can be assumed.
      Type Parameters:
      T - Return type
      Parameters:
      aSupplier - Callable to be executed. May not be null.
      Returns:
      The return value of the callable. May be null.
    • lockedGetThrowing

      public <T, EXTYPE extends Exception> T lockedGetThrowing(@Nonnull IThrowingSupplier<? extends T,EXTYPE> aCallable) throws EXTYPE
      Execute the provided callable in a read lock. Note: no nullable/non-nullable can be assumed.
      Type Parameters:
      T - Return type
      EXTYPE - Exception type to be thrown
      Parameters:
      aCallable - Callable to be executed. May not be null.
      Returns:
      The return value of the callable. May be null.
      Throws:
      EXTYPE - If the callable throws the exception
    • lockedBoolean

      public boolean lockedBoolean(@Nonnull BooleanSupplier aSupplier)
      Execute the provided callable in a read lock.
      Parameters:
      aSupplier - Callable to be executed. May not be null.
      Returns:
      The return value of the callable. May be null.
    • lockedDouble

      public double lockedDouble(@Nonnull DoubleSupplier aSupplier)
      Execute the provided callable in a read lock.
      Parameters:
      aSupplier - Callable to be executed. May not be null.
      Returns:
      The return value of the callable. May be null.
    • lockedInt

      public int lockedInt(@Nonnull IntSupplier aSupplier)
      Execute the provided callable in a read lock.
      Parameters:
      aSupplier - Callable to be executed. May not be null.
      Returns:
      The return value of the callable. May be null.
    • lockedLong

      public long lockedLong(@Nonnull LongSupplier aSupplier)
      Execute the provided callable in a read lock.
      Parameters:
      aSupplier - Callable to be executed. May not be null.
      Returns:
      The return value of the callable. May be null.