Class SimpleReadWriteLock

java.lang.Object
java.util.concurrent.locks.ReentrantReadWriteLock
com.helger.commons.concurrent.SimpleReadWriteLock
All Implemented Interfaces:
Serializable, ReadWriteLock

public class SimpleReadWriteLock extends ReentrantReadWriteLock
This is an extension around ReentrantReadWriteLock that allows for easy usage with Java 8 :) See readLocked(Runnable) and writeLocked(Runnable) method. For throwing versions see readLockedThrowing(IThrowingRunnable) and writeLockedThrowing(IThrowingRunnable). Also methods for callables are available.
Author:
Philip Helger
See Also:
  • Constructor Details

    • SimpleReadWriteLock

      public SimpleReadWriteLock()
      Default constructor creating a default ReentrantReadWriteLock
    • SimpleReadWriteLock

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

    • readLockedGet

      public <T> T readLockedGet(@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.
    • readLocked

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

      public <EXTYPE extends Exception> void readLockedThrowing(@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
    • readLockedGetThrowing

      public <T, EXTYPE extends Exception> T readLockedGetThrowing(@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
    • readLockedBoolean

      public boolean readLockedBoolean(@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.
    • readLockedDouble

      public double readLockedDouble(@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.
    • readLockedInt

      public int readLockedInt(@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.
    • readLockedLong

      public long readLockedLong(@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.
    • writeLocked

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

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

      public <T> T writeLockedGet(@Nonnull Supplier<? extends T> aSupplier)
      Execute the provided callable in a write 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.
    • writeLockedGetThrowing

      public <T, EXTYPE extends Exception> T writeLockedGetThrowing(@Nonnull IThrowingSupplier<? extends T,EXTYPE> aCallable) throws EXTYPE
      Execute the provided callable in a write 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
    • writeLockedBoolean

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

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

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

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