Package org.kiwiproject.concurrent
Class TryLocker
java.lang.Object
org.kiwiproject.concurrent.TryLocker
Utility that aids in using
Lock.tryLock(long, TimeUnit).
This can also make unit testing easier by allowing easy mocking.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default lock wait time if not specified.static final TimeUnitThe default lock wait time unit if not specified. -
Method Summary
Modifier and TypeMethodDescriptionReturn the maximum lock wait time as aDuration.longstatic TryLockerCreate a newTryLockerusing the given lock and maximum wait time.static TryLockerCreate a newTryLockerusing aReentrantLockand the default lock maximum wait time.static TryLockerusingReentrantLock(int maxWaitTime, TimeUnit maxWaitTimeUnit) Create a newTryLockerusing aReentrantLockwith the given lock wait time.voidwithLockOrElse(Runnable withLockAction, Runnable orElseAction) RunwithLockActionif the lock is obtained within the lock timeout period.<T> TwithLockSupplyOrFallback(Supplier<T> withLockSupplier, T fallbackValue) Execute the givenwithLockSupplierif the lock is obtained within the lock timeout period and return its value.<T> TwithLockSupplyOrFallbackSupply(Supplier<T> withLockSupplier, Supplier<T> fallbackSupplier) Execute the givenwithLockSupplierif the lock is obtained within the lock timeout period and return its value.<T> TwithLockSupplyOrNull(Supplier<T> withLockSupplier) Execute the givenwithLockSupplierif the lock is obtained within the lock timeout period and return its value.
-
Field Details
-
DEFAULT_LOCK_WAIT_TIME
public static final int DEFAULT_LOCK_WAIT_TIMEThe default lock wait time if not specified.- See Also:
-
DEFAULT_LOCK_WAIT_TIME_UNIT
The default lock wait time unit if not specified.
-
-
Method Details
-
usingReentrantLock
Create a newTryLockerusing aReentrantLockand the default lock maximum wait time.- Returns:
- a new instance
- See Also:
-
usingReentrantLock
Create a newTryLockerusing aReentrantLockwith the given lock wait time.- Parameters:
maxWaitTime- maximum lock wait timemaxWaitTimeUnit- maximum lock wait time value- Returns:
- a new instance
-
using
Create a newTryLockerusing the given lock and maximum wait time.- Parameters:
lock- theLockto usemaxWaitTime- maximum lock wait timemaxWaitTimeUnit- maximum lock wait time value- Returns:
- a new instance
-
getLockWaitDuration
Return the maximum lock wait time as aDuration.- Returns:
- the lock wait duration
-
withLockOrElse
RunwithLockActionif the lock is obtained within the lock timeout period. Otherwise, runorElseAction.- Parameters:
withLockAction- action to run if lock is obtainedorElseAction- action to run if lock is not obtained
-
withLockSupplyOrNull
Execute the givenwithLockSupplierif the lock is obtained within the lock timeout period and return its value. Otherwise, return null.- Type Parameters:
T- type of object returned- Parameters:
withLockSupplier- supplier to execute if lock is obtained- Returns:
- the supplied value or null
-
withLockSupplyOrFallback
Execute the givenwithLockSupplierif the lock is obtained within the lock timeout period and return its value. Otherwise, return thefallbackValue.- Type Parameters:
T- type of object returned- Parameters:
withLockSupplier- supplier to execute if lock is obtainedfallbackValue- the value to use if the lock is not obtained- Returns:
- the supplied value or the fallback value
-
withLockSupplyOrFallbackSupply
public <T> T withLockSupplyOrFallbackSupply(Supplier<T> withLockSupplier, Supplier<T> fallbackSupplier) Execute the givenwithLockSupplierif the lock is obtained within the lock timeout period and return its value. Otherwise, return the valued supplied byfallbackSupplier.- Type Parameters:
T- type of object returned- Parameters:
withLockSupplier- supplier to execute if lock is obtainedfallbackSupplier- the supplier to execute if the lock is not obtained- Returns:
- the supplied value or the supplied fallback value
-
getLockWaitTime
public long getLockWaitTime() -
getLockWaitTimeUnit
-