Package com.helger.commons.concurrent
Class SimpleLock
java.lang.Object
java.util.concurrent.locks.ReentrantLock
com.helger.commons.concurrent.SimpleLock
- All Implemented Interfaces:
Serializable,Lock
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 Summary
ConstructorsConstructorDescriptionDefault constructor creating a defaultReentrantLockSimpleLock(boolean bFair) Constructor creating aReentrantLockwith the provided fairness -
Method Summary
Modifier and TypeMethodDescriptionvoidExecute the provided runnable in a read lock.booleanlockedBoolean(BooleanSupplier aSupplier) Execute the provided callable in a read lock.doublelockedDouble(DoubleSupplier aSupplier) Execute the provided callable in a read lock.<T> TExecute the provided callable in a read lock.<T,EXTYPE extends Exception>
TlockedGetThrowing(IThrowingSupplier<? extends T, EXTYPE> aCallable) Execute the provided callable in a read lock.intlockedInt(IntSupplier aSupplier) Execute the provided callable in a read lock.longlockedLong(LongSupplier aSupplier) Execute the provided callable in a read lock.<EXTYPE extends Exception>
voidlockedThrowing(IThrowingRunnable<EXTYPE> aRunnable) Execute the provided runnable in a read lock.Methods inherited from class java.util.concurrent.locks.ReentrantLock
getHoldCount, getOwner, getQueuedThreads, getQueueLength, getWaitingThreads, getWaitQueueLength, hasQueuedThread, hasQueuedThreads, hasWaiters, isFair, isHeldByCurrentThread, isLocked, lock, lockInterruptibly, newCondition, toString, tryLock, tryLock, unlock
-
Constructor Details
-
SimpleLock
public SimpleLock()Default constructor creating a defaultReentrantLock -
SimpleLock
public SimpleLock(boolean bFair) Constructor creating aReentrantLockwith the provided fairness- Parameters:
bFair-trueif this lock should use a fair ordering policy
-
-
Method Details
-
locked
Execute the provided runnable in a read lock.- Parameters:
aRunnable- Runnable to be executed. May not benull.
-
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 benull.- Throws:
EXTYPE- If the callable throws the exception
-
lockedGet
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.
-
lockedGetThrowing
public <T,EXTYPE extends Exception> T lockedGetThrowing(@Nonnull IThrowingSupplier<? extends T, EXTYPE> aCallable) throws EXTYPEExecute 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 exception
-
lockedBoolean
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.
-
lockedDouble
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.
-
lockedInt
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.
-
lockedLong
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.
-