Package com.helger.commons.concurrent
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 aroundReentrantReadWriteLockthat allows for easy usage with Java 8 :) SeereadLocked(Runnable)andwriteLocked(Runnable)method. For throwing versions seereadLockedThrowing(IThrowingRunnable)andwriteLockedThrowing(IThrowingRunnable). Also methods for callables are available.- Author:
- Philip Helger
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.locks.ReentrantReadWriteLock
ReentrantReadWriteLock.ReadLock, ReentrantReadWriteLock.WriteLock
-
-
Constructor Summary
Constructors Constructor Description SimpleReadWriteLock()Default constructor creating a defaultReentrantReadWriteLockSimpleReadWriteLock(boolean bFair)Constructor creating aReentrantReadWriteLockwith the provided fairness
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidreadLocked(Runnable aRunnable)Execute the provided runnable in a read lock.booleanreadLockedBoolean(BooleanSupplier aSupplier)Execute the provided callable in a read lock.doublereadLockedDouble(DoubleSupplier aSupplier)Execute the provided callable in a read lock.<T> TreadLockedGet(Supplier<? extends T> aSupplier)Execute the provided callable in a read lock.<T,EXTYPE extends Exception>
TreadLockedGetThrowing(IThrowingSupplier<? extends T,EXTYPE> aCallable)Execute the provided callable in a read lock.intreadLockedInt(IntSupplier aSupplier)Execute the provided callable in a read lock.longreadLockedLong(LongSupplier aSupplier)Execute the provided callable in a read lock.<EXTYPE extends Exception>
voidreadLockedThrowing(IThrowingRunnable<EXTYPE> aRunnable)Execute the provided runnable in a read lock.voidwriteLocked(Runnable aRunnable)Execute the provided runnable in a write lock.booleanwriteLockedBoolean(BooleanSupplier aSupplier)Execute the provided callable in a write lock.doublewriteLockedDouble(DoubleSupplier aSupplier)Execute the provided callable in a write lock.<T> TwriteLockedGet(Supplier<? extends T> aSupplier)Execute the provided callable in a write lock.<T,EXTYPE extends Exception>
TwriteLockedGetThrowing(IThrowingSupplier<? extends T,EXTYPE> aCallable)Execute the provided callable in a write lock.intwriteLockedInt(IntSupplier aSupplier)Execute the provided callable in a write lock.longwriteLockedLong(LongSupplier aSupplier)Execute the provided callable in a write lock.<EXTYPE extends Exception>
voidwriteLockedThrowing(IThrowingRunnable<EXTYPE> aRunnable)Execute the provided runnable in a write lock.-
Methods inherited from class java.util.concurrent.locks.ReentrantReadWriteLock
getOwner, getQueuedReaderThreads, getQueuedThreads, getQueuedWriterThreads, getQueueLength, getReadHoldCount, getReadLockCount, getWaitingThreads, getWaitQueueLength, getWriteHoldCount, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isWriteLocked, isWriteLockedByCurrentThread, readLock, toString, writeLock
-
-
-
-
Constructor Detail
-
SimpleReadWriteLock
public SimpleReadWriteLock()
Default constructor creating a defaultReentrantReadWriteLock
-
SimpleReadWriteLock
public SimpleReadWriteLock(boolean bFair)
Constructor creating aReentrantReadWriteLockwith the provided fairness- Parameters:
bFair-trueif this lock should use a fair ordering policy
-
-
Method Detail
-
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 benull.- 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 benull.
-
readLockedThrowing
public <EXTYPE extends Exception> void readLockedThrowing(@Nonnull IThrowingRunnable<EXTYPE> aRunnable) throws EXTYPE extends Exception
Execute the provided runnable in a read lock.- Type Parameters:
EXTYPE- Exception type to be thrown- Parameters:
aRunnable- Runnable to be executed. May not benull.- Throws:
EXTYPE- If the callable throws the exceptionEXTYPE extends Exception
-
readLockedGetThrowing
public <T,EXTYPE extends Exception> T readLockedGetThrowing(@Nonnull IThrowingSupplier<? extends T,EXTYPE> aCallable) throws EXTYPE extends Exception
Execute the provided callable in a read lock. Note: no nullable/non-nullable can be assumed.- Type Parameters:
T- Return typeEXTYPE- Exception type to be thrown- Parameters:
aCallable- Callable to be executed. May not benull.- Returns:
- The return value of the callable. May be
null. - Throws:
EXTYPE- If the callable throws the exceptionEXTYPE extends Exception
-
readLockedBoolean
public boolean readLockedBoolean(@Nonnull BooleanSupplier aSupplier)
Execute the provided callable in a read lock.- Parameters:
aSupplier- Callable to be executed. May not benull.- 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 benull.- 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 benull.- 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 benull.- 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 benull.
-
writeLockedThrowing
public <EXTYPE extends Exception> void writeLockedThrowing(@Nonnull IThrowingRunnable<EXTYPE> aRunnable) throws EXTYPE extends Exception
Execute the provided runnable in a write lock.- Type Parameters:
EXTYPE- Exception type to be thrown- Parameters:
aRunnable- Runnable to be executed. May not benull.- Throws:
EXTYPE- If the runnable throws the exceptionEXTYPE extends 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 benull.- 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 extends Exception
Execute the provided callable in a write lock. Note: no nullable/non-nullable can be assumed.- Type Parameters:
T- Return typeEXTYPE- Exception type to be thrown- Parameters:
aCallable- Callable to be executed. May not benull.- Returns:
- The return value of the callable. May be
null. - Throws:
EXTYPE- If the callable throws the exceptionEXTYPE extends Exception
-
writeLockedBoolean
public boolean writeLockedBoolean(@Nonnull BooleanSupplier aSupplier)
Execute the provided callable in a write lock.- Parameters:
aSupplier- Callable to be executed. May not benull.- 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 benull.- 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 benull.- 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 benull.- Returns:
- The return value of the callable. May be
null.
-
-