Package org.infinispan.lock.api
Interface ClusteredLock
public interface ClusteredLock
ClusteredLock is a data structure used for concurrent programming between Infinispan instances in cluster mode.
A typical usage idiom for
lock()
will be :
ClusteredLock lock = clm.get("lock");
lock.lock()
.thenRun(() ->
try {
// manipulate protected state
} finally {
return lock.unlock();
}
)
A typical usage idiom for tryLock()
will be :
lock.tryLock()
.thenCompose(result -> {
if (result) {
try {
// manipulate protected state
} finally {
return lock.unlock();
}
} else {
// Do something else
}
});
- Since:
- 9.2
- Author:
- Katia Aresti, karesti@redhat.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionisLocked()
Returns aCompletableFuture<Boolean>
holdingtrue
when the lock is locked andfalse
when the lock is released.Returns aCompletableFuture<Boolean>
holdingtrue
when the lock is owned by the caller andfalse
when the lock is owned by someone else or it's released.lock()
Acquires the lock.tryLock()
Acquires the lock only if it is free at the time of invocation.If the lock is available this method returns immediately with theCompletableFuture
holding the valuetrue
.unlock()
Releases the lock.
-
Method Details
-
lock
CompletableFuture<Void> lock()Acquires the lock. If the lock is not available then theCompletableFuture
waits until the lock has been acquired. Currently, there is no maximum time specified for a lock request to fail, so this could cause thread starvation.- Returns:
- a completed
CompletableFuture
when the lock is acquired - Throws:
ClusteredLockException
- when the lock does not exist
-
tryLock
CompletableFuture<Boolean> tryLock()Acquires the lock only if it is free at the time of invocation. Acquires the lock if it is available and returns immediately with with theCompletableFuture
holding the valuetrue
. If the lock is not available then this method will return immediately with theCompletableFuture
holding the valuefalse
.- Returns:
CompletableFuture(true)
if the lock was acquired andCompletableFuture(false)
otherwise- Throws:
ClusteredLockException
- when the lock does not exist
-
tryLock
If the lock is available this method returns immediately with theCompletableFuture
holding the valuetrue
. If the lock is not available then theCompletableFuture
waits until :- The lock is acquired
- The specified waiting time elapses
CompletableFuture
will complete with the valuetrue
. If the specified waiting time elapses then theCompletableFuture
will complete with the valuefalse
. If the time is less than or equal to zero, the method will not wait at all.- Parameters:
time
- , the maximum time to wait for the lockunit
- , the time unit of thetime
argument- Returns:
CompletableFuture(true)
if the lock was acquired andCompletableFuture(false)
if the waiting time elapsed before the lock was acquired- Throws:
ClusteredLockException
- when the lock does not exist
-
unlock
CompletableFuture<Void> unlock()Releases the lock. Only the holder of the lock may release the lock.- Returns:
- a completed
CompletableFuture
when the lock is released - Throws:
ClusteredLockException
- when the lock does not exist
-
isLocked
CompletableFuture<Boolean> isLocked()Returns aCompletableFuture<Boolean>
holdingtrue
when the lock is locked andfalse
when the lock is released.- Returns:
- a
CompletableFuture
holding aBoolean
- Throws:
ClusteredLockException
- when the lock does not exist
-
isLockedByMe
CompletableFuture<Boolean> isLockedByMe()Returns aCompletableFuture<Boolean>
holdingtrue
when the lock is owned by the caller andfalse
when the lock is owned by someone else or it's released.- Returns:
- a
CompletableFuture
holding aBoolean
- Throws:
ClusteredLockException
- when the lock does not exist
-