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
public class SimpleLock extends ReentrantLock
This is an extension aroundReentrantLockthat allows for easy usage with Java 8 :) Seelocked(Runnable)andlocked(Runnable)method. Also methods for suppliers are available.- Author:
- Philip Helger
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SimpleLock()Default constructor creating a defaultReentrantLockSimpleLock(boolean bFair)Constructor creating aReentrantLockwith the provided fairness
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidlocked(Runnable aRunnable)Execute 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> TlockedGet(Supplier<? extends T> aSupplier)Execute 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 Detail
-
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 Detail
-
locked
public void locked(@Nonnull Runnable aRunnable)
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 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
-
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 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 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
-
lockedBoolean
public boolean lockedBoolean(@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.
-
lockedDouble
public double lockedDouble(@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.
-
lockedInt
public int lockedInt(@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.
-
lockedLong
public long lockedLong(@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.
-
-