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 forlock()
will be :ClusteredLock lock = clm.get("lock"); lock.lock() .thenRun(() -> try { // manipulate protected state } finally { return lock.unlock(); } )
A typical usage idiom fortryLock()
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:
- Infinispan documentation
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description CompletableFuture<Boolean>
isLocked()
Returns aCompletableFuture
holdingtrue
when the lock is locked andfalse
when the lock is released.CompletableFuture<Boolean>
isLockedByMe()
Returns aCompletableFuture
holdingtrue
when the lock is owned by the caller andfalse
when the lock is owned by someone else or it's released.CompletableFuture<Void>
lock()
Acquires the lock.CompletableFuture<Boolean>
tryLock()
Acquires the lock only if it is free at the time of invocation.CompletableFuture<Boolean>
tryLock(long time, TimeUnit unit)
If the lock is available this method returns immediately with theCompletableFuture
holding the valuetrue
.CompletableFuture<Void>
unlock()
Releases the lock.
-
-
-
Method Detail
-
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
CompletableFuture<Boolean> tryLock(long time, TimeUnit unit)
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
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
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
-
-