Package com.glitchybyte.glib.concurrent
Class GLock
java.lang.Object
com.glitchybyte.glib.concurrent.GLock
Lock utilities.
This utility class contains common locking constructs.
These utilities are not meant to replace all use cases. They encapsulate common quick operations. They are especially unsuited for operations that throw exceptions.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidawaitCondition(Lock lock, Condition condition) Acquire lock and awaits on the condition.static voidawaitConditionWithTest(Lock lock, Condition condition, BooleanSupplier test) Acquire lock and awaits on the condition until test is true.static booleanawaitConditionWithTestAndTimeout(Lock lock, Condition condition, BooleanSupplier test, Duration timeout) Acquire lock and awaits on the condition until test is true or timeout expires.static booleanawaitConditionWithTimeout(Lock lock, Condition condition, Duration timeout) Acquire lock and awaits on the condition up to the given timeout.static voidRun a block of code between lock and unlock.static <V> VlockedResult(Lock lock, Supplier<V> supplier) Run a block of code, with a return value, between lock and unlock.static voidreadLocked(ReadWriteLock lock, Runnable runnable) Run a block of code between a read-lock and unlock.static <V> VreadLockedResult(ReadWriteLock lock, Supplier<V> supplier) Run a block of code, with a return value, between a read-lock and unlock.static voidSignal all monitors on the given condition.static voidwriteLocked(ReadWriteLock lock, Runnable runnable) Run a block of code between a write-lock and unlock.static <V> VwriteLockedResult(ReadWriteLock lock, Supplier<V> supplier) Run a block of code, with a return value, between a write-lock and unlock.
-
Method Details
-
locked
Run a block of code between lock and unlock.- Parameters:
lock- Lock.runnable- Block of code.
-
lockedResult
Run a block of code, with a return value, between lock and unlock.- Type Parameters:
V- Type of the return value.- Parameters:
lock- Lock.supplier- Block of code.- Returns:
- Value returned by the block.
-
awaitCondition
Acquire lock and awaits on the condition.- Parameters:
lock- Lock.condition- Condition.- Throws:
InterruptedException- If interrupted while awaiting.
-
awaitConditionWithTest
public static void awaitConditionWithTest(Lock lock, Condition condition, BooleanSupplier test) throws InterruptedException Acquire lock and awaits on the condition until test is true.- Parameters:
lock- Lock.condition- Condition.test- Test to await for.- Throws:
InterruptedException- If interrupted while awaiting.
-
awaitConditionWithTimeout
public static boolean awaitConditionWithTimeout(Lock lock, Condition condition, Duration timeout) throws InterruptedException Acquire lock and awaits on the condition up to the given timeout.- Parameters:
lock- Lock.condition- Condition.timeout- Await timeout.- Returns:
- True if condition was signaled. False if await timed out.
- Throws:
InterruptedException- If interrupted while awaiting.
-
awaitConditionWithTestAndTimeout
public static boolean awaitConditionWithTestAndTimeout(Lock lock, Condition condition, BooleanSupplier test, Duration timeout) throws InterruptedException Acquire lock and awaits on the condition until test is true or timeout expires.- Parameters:
lock- Lock.condition- Condition.test- Test to await for.timeout- Await timeout.- Returns:
- True if test is true.
- Throws:
InterruptedException- If interrupted while awaiting.
-
signalAll
Signal all monitors on the given condition.- Parameters:
lock- Lock.condition- Condition.
-
readLocked
Run a block of code between a read-lock and unlock.- Parameters:
lock- Read/write lock.runnable- Block of code.
-
readLockedResult
Run a block of code, with a return value, between a read-lock and unlock.- Type Parameters:
V- Type of the return value.- Parameters:
lock- Lock.supplier- Block of code.- Returns:
- Value returned by the block.
-
writeLocked
Run a block of code between a write-lock and unlock.- Parameters:
lock- Read/write lock.runnable- Block of code.
-
writeLockedResult
Run a block of code, with a return value, between a write-lock and unlock.- Type Parameters:
V- Type of the return value.- Parameters:
lock- Lock.supplier- Block of code.- Returns:
- Value returned by the block.
-