Class CloseableReentrantReadWriteLock
java.lang.Object
com.inductiveautomation.ignition.common.util.CloseableReentrantReadWriteLock
A wrapper around
ReentrantReadWriteLock that provides auto-closeable lock acquisition for
use with try-with-resources.
Example usage:
private final CloseableLock lock = new CloseableLock(false);
public void doSomethingUnderReadLock() {
try (var ignored = lock.acquireReadLock()) {
// ... code that runs while holding the read lock ...
}
}
public void doSomethingUnderWriteLock() {
try (var ignored = lock.acquireWriteLock()) {
// ... code that runs while holding the write lock ...
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAnAutoCloseablethat releases a lock when closed. -
Constructor Summary
ConstructorsConstructorDescriptionCloseableReentrantReadWriteLock(boolean fair) Creates a new CloseableLock with the specified fairness policy. -
Method Summary
Modifier and TypeMethodDescriptionAcquires the given lock and returns anCloseableReentrantReadWriteLock.CloseableLockthat will release it when closed.Acquires the read lock and returns anCloseableReentrantReadWriteLock.CloseableLockthat will release it when closed.Acquires the write lock and returns anCloseableReentrantReadWriteLock.CloseableLockthat will release it when closed.
-
Constructor Details
-
CloseableReentrantReadWriteLock
public CloseableReentrantReadWriteLock(boolean fair) Creates a new CloseableLock with the specified fairness policy.- Parameters:
fair-trueif this lock should use a fair ordering policy
-
-
Method Details
-
acquireReadLock
Acquires the read lock and returns anCloseableReentrantReadWriteLock.CloseableLockthat will release it when closed.- Returns:
- An Unlock that releases the read lock when closed
-
acquireWriteLock
Acquires the write lock and returns anCloseableReentrantReadWriteLock.CloseableLockthat will release it when closed.- Returns:
- An Unlock that releases the write lock when closed
-
acquire
Acquires the given lock and returns anCloseableReentrantReadWriteLock.CloseableLockthat will release it when closed. This static method is useful for wrapping arbitraryLockinstances.Example usage:
Lock someLock = ...; try (var ignored = CloseableLock.acquire(someLock)) { // ... code that runs while holding the lock ... }- Parameters:
lock- The lock to acquire- Returns:
- An Unlock that releases the lock when closed
-