Package org.kiwiproject.concurrent
Class TryLocker
- java.lang.Object
-
- org.kiwiproject.concurrent.TryLocker
-
public class TryLocker extends Object
Utility that aids in usingLock.tryLock(long, TimeUnit).This can also make unit testing easier by allowing easy mocking.
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_LOCK_WAIT_TIMEThe default lock wait time if not specified.static TimeUnitDEFAULT_LOCK_WAIT_TIME_UNITThe default lock wait time unit if not specified.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DurationgetLockWaitDuration()Return the maximum lock wait time as aDuration.static TryLockerusing(Lock lock, long maxWaitTime, TimeUnit maxWaitTimeUnit)Create a newTryLockerusing the given lock and maximum wait time.static TryLockerusingReentrantLock()Create 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 Detail
-
DEFAULT_LOCK_WAIT_TIME
public static final int DEFAULT_LOCK_WAIT_TIME
The default lock wait time if not specified.- See Also:
- Constant Field Values
-
DEFAULT_LOCK_WAIT_TIME_UNIT
public static final TimeUnit DEFAULT_LOCK_WAIT_TIME_UNIT
The default lock wait time unit if not specified.
-
-
Method Detail
-
usingReentrantLock
public static TryLocker usingReentrantLock()
Create a newTryLockerusing aReentrantLockand the default lock maximum wait time.- Returns:
- a new instance
- See Also:
DEFAULT_LOCK_WAIT_TIME,DEFAULT_LOCK_WAIT_TIME_UNIT
-
usingReentrantLock
public static TryLocker usingReentrantLock(int maxWaitTime, TimeUnit maxWaitTimeUnit)
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
public static TryLocker using(Lock lock, long maxWaitTime, TimeUnit maxWaitTimeUnit)
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
public Duration getLockWaitDuration()
Return the maximum lock wait time as aDuration.- Returns:
- the lock wait duration
-
withLockOrElse
public void withLockOrElse(Runnable withLockAction, Runnable orElseAction)
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
public <T> T withLockSupplyOrNull(Supplier<T> withLockSupplier)
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
public <T> T withLockSupplyOrFallback(Supplier<T> withLockSupplier, T fallbackValue)
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
-
-