Package org.redisson

Class RedissonMultiLock

  • All Implemented Interfaces:
    Lock, RLock, RLockAsync
    Direct Known Subclasses:
    RedissonRedLock

    public class RedissonMultiLock
    extends Object
    implements RLock
    Groups multiple independent locks and manages them as one lock.
    Author:
    Nikita Koksharov
    • Constructor Detail

      • RedissonMultiLock

        public RedissonMultiLock​(RLock... locks)
        Creates instance with multiple RLock objects. Each RLock object could be created by own Redisson instance.
        Parameters:
        locks - - array of locks
    • Method Detail

      • lock

        public void lock()
        Specified by:
        lock in interface Lock
      • lock

        public void lock​(long leaseTime,
                         TimeUnit unit)
        Description copied from interface: RLock
        Acquires the lock with defined leaseTime. Waits if necessary until lock became available. Lock will be released automatically after defined leaseTime interval.
        Specified by:
        lock in interface RLock
        Parameters:
        leaseTime - the maximum time to hold the lock after it's acquisition, if it hasn't already been released by invoking unlock. If leaseTime is -1, hold the lock until explicitly unlocked.
        unit - the time unit
      • lockAsync

        public RFuture<Void> lockAsync​(long leaseTime,
                                       TimeUnit unit)
        Description copied from interface: RLockAsync
        Acquires the lock with defined leaseTime. Waits if necessary until lock became available. Lock will be released automatically after defined leaseTime interval.
        Specified by:
        lockAsync in interface RLockAsync
        Parameters:
        leaseTime - the maximum time to hold the lock after it's acquisition, if it hasn't already been released by invoking unlock. If leaseTime is -1, hold the lock until explicitly unlocked.
        unit - the time unit
        Returns:
        void
      • lockAsync

        public RFuture<Void> lockAsync​(long leaseTime,
                                       TimeUnit unit,
                                       long threadId)
        Description copied from interface: RLockAsync
        Acquires the lock with defined leaseTime and threadId. Waits if necessary until lock became available. Lock will be released automatically after defined leaseTime interval.
        Specified by:
        lockAsync in interface RLockAsync
        Parameters:
        leaseTime - the maximum time to hold the lock after it's acquisition, if it hasn't already been released by invoking unlock. If leaseTime is -1, hold the lock until explicitly unlocked.
        unit - the time unit
        threadId - id of thread
        Returns:
        void
      • tryLockAsync

        protected void tryLockAsync​(long threadId,
                                    long leaseTime,
                                    TimeUnit unit,
                                    long waitTime,
                                    RPromise<Void> result)
      • lockInterruptibly

        public void lockInterruptibly​(long leaseTime,
                                      TimeUnit unit)
                               throws InterruptedException
        Description copied from interface: RLock
        Acquires the lock with defined leaseTime. Waits if necessary until lock became available. Lock will be released automatically after defined leaseTime interval.
        Specified by:
        lockInterruptibly in interface RLock
        Parameters:
        leaseTime - the maximum time to hold the lock after it's acquisition, if it hasn't already been released by invoking unlock. If leaseTime is -1, hold the lock until explicitly unlocked.
        unit - the time unit
        Throws:
        InterruptedException - - if the thread is interrupted
      • tryLock

        public boolean tryLock()
        Specified by:
        tryLock in interface Lock
      • failedLocksLimit

        protected int failedLocksLimit()
      • tryLock

        public boolean tryLock​(long waitTime,
                               long leaseTime,
                               TimeUnit unit)
                        throws InterruptedException
        Description copied from interface: RLock
        Tries to acquire the lock with defined leaseTime. Waits up to defined waitTime if necessary until the lock became available. Lock will be released automatically after defined leaseTime interval.
        Specified by:
        tryLock in interface RLock
        Parameters:
        waitTime - the maximum time to acquire the lock
        leaseTime - lease time
        unit - time unit
        Returns:
        true if lock is successfully acquired, otherwise false if lock is already set.
        Throws:
        InterruptedException - - if the thread is interrupted
      • tryLockAsync

        public RFuture<Boolean> tryLockAsync​(long waitTime,
                                             long leaseTime,
                                             TimeUnit unit,
                                             long threadId)
        Description copied from interface: RLockAsync
        Tries to acquire the lock by thread with specified threadId and leaseTime. Waits up to defined waitTime if necessary until the lock became available. Lock will be released automatically after defined leaseTime interval.
        Specified by:
        tryLockAsync in interface RLockAsync
        Parameters:
        waitTime - time interval to acquire lock
        leaseTime - time interval after which lock will be released automatically
        unit - the time unit of the waitTime and leaseTime arguments
        threadId - id of thread
        Returns:
        true if lock acquired otherwise false
      • tryLockAsync

        public RFuture<Boolean> tryLockAsync​(long waitTime,
                                             long leaseTime,
                                             TimeUnit unit)
        Description copied from interface: RLockAsync
        Tries to acquire the lock with defined leaseTime. Waits up to defined waitTime if necessary until the lock became available. Lock will be released automatically after defined leaseTime interval.
        Specified by:
        tryLockAsync in interface RLockAsync
        Parameters:
        waitTime - the maximum time to acquire the lock
        leaseTime - lease time
        unit - time unit
        Returns:
        true if lock is successfully acquired, otherwise false if lock is already set.
      • calcLockWaitTime

        protected long calcLockWaitTime​(long remainTime)
      • unlock

        public void unlock()
        Specified by:
        unlock in interface Lock
      • forceUnlockAsync

        public RFuture<Boolean> forceUnlockAsync()
        Description copied from interface: RLockAsync
        Unlocks the lock independently of its state
        Specified by:
        forceUnlockAsync in interface RLockAsync
        Returns:
        true if lock existed and now unlocked otherwise false
      • lockAsync

        public RFuture<Void> lockAsync()
        Description copied from interface: RLockAsync
        Acquires the lock. Waits if necessary until lock became available.
        Specified by:
        lockAsync in interface RLockAsync
        Returns:
        void
      • lockAsync

        public RFuture<Void> lockAsync​(long threadId)
        Description copied from interface: RLockAsync
        Acquires the lock by thread with defined threadId. Waits if necessary until lock became available.
        Specified by:
        lockAsync in interface RLockAsync
        Parameters:
        threadId - id of thread
        Returns:
        void
      • tryLockAsync

        public RFuture<Boolean> tryLockAsync​(long threadId)
        Description copied from interface: RLockAsync
        Tries to acquire the lock by thread with specified threadId.
        Specified by:
        tryLockAsync in interface RLockAsync
        Parameters:
        threadId - id of thread
        Returns:
        true if lock acquired otherwise false
      • tryLockAsync

        public RFuture<Boolean> tryLockAsync​(long waitTime,
                                             TimeUnit unit)
        Description copied from interface: RLockAsync
        Tries to acquire the lock. Waits up to defined waitTime if necessary until the lock became available.
        Specified by:
        tryLockAsync in interface RLockAsync
        Parameters:
        waitTime - the maximum time to acquire the lock
        unit - time unit
        Returns:
        true if lock is successfully acquired, otherwise false if lock is already set.
      • getHoldCountAsync

        public RFuture<Integer> getHoldCountAsync()
        Description copied from interface: RLockAsync
        Number of holds on this lock by the current thread
        Specified by:
        getHoldCountAsync in interface RLockAsync
        Returns:
        holds or 0 if this lock is not held by current thread
      • getName

        public String getName()
        Description copied from interface: RLock
        Returns name of object
        Specified by:
        getName in interface RLock
        Returns:
        name - name of object
      • forceUnlock

        public boolean forceUnlock()
        Description copied from interface: RLock
        Unlocks the lock independently of its state
        Specified by:
        forceUnlock in interface RLock
        Returns:
        true if lock existed and now unlocked otherwise false
      • isLocked

        public boolean isLocked()
        Description copied from interface: RLock
        Checks if the lock locked by any thread
        Specified by:
        isLocked in interface RLock
        Returns:
        true if locked otherwise false
      • isLockedAsync

        public RFuture<Boolean> isLockedAsync()
        Description copied from interface: RLockAsync
        Checks if the lock locked by any thread
        Specified by:
        isLockedAsync in interface RLockAsync
        Returns:
        true if locked otherwise false
      • isHeldByThread

        public boolean isHeldByThread​(long threadId)
        Description copied from interface: RLock
        Checks if the lock is held by thread with defined threadId
        Specified by:
        isHeldByThread in interface RLock
        Parameters:
        threadId - Thread ID of locking thread
        Returns:
        true if held by thread with given id otherwise false
      • isHeldByCurrentThread

        public boolean isHeldByCurrentThread()
        Description copied from interface: RLock
        Checks if this lock is held by the current thread
        Specified by:
        isHeldByCurrentThread in interface RLock
        Returns:
        true if held by current thread otherwise false
      • getHoldCount

        public int getHoldCount()
        Description copied from interface: RLock
        Number of holds on this lock by the current thread
        Specified by:
        getHoldCount in interface RLock
        Returns:
        holds or 0 if this lock is not held by current thread
      • remainTimeToLiveAsync

        public RFuture<Long> remainTimeToLiveAsync()
        Description copied from interface: RLockAsync
        Remaining time to live of the lock
        Specified by:
        remainTimeToLiveAsync in interface RLockAsync
        Returns:
        time in milliseconds -2 if the lock does not exist. -1 if the lock exists but has no associated expire.
      • remainTimeToLive

        public long remainTimeToLive()
        Description copied from interface: RLock
        Remaining time to live of the lock
        Specified by:
        remainTimeToLive in interface RLock
        Returns:
        time in milliseconds -2 if the lock does not exist. -1 if the lock exists but has no associated expire.