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:
    Infinispan documentation
    • Method Detail

      • lock

        CompletableFuture<Void> lock()
        Acquires the lock. If the lock is not available then the CompletableFuture 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 the CompletableFuture holding the value true. If the lock is not available then this method will return immediately with the CompletableFuture holding the value false.
        Returns:
        CompletableFuture(true) if the lock was acquired and CompletableFuture(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 the CompletableFuture holding the value true. If the lock is not available then the CompletableFuture waits until :
        • The lock is acquired
        • The specified waiting time elapses
        If the lock is acquired then the CompletableFuture will complete with the value true. If the specified waiting time elapses then the CompletableFuture will complete with the value false. 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 lock
        unit - , the time unit of the time argument
        Returns:
        CompletableFuture(true) if the lock was acquired and CompletableFuture(false) if the waiting time elapsed before the lock was acquired
        Throws:
        ClusteredLockException - when the lock does not exist