preface

The Redisson multilock object can associate multiple RLock objects into an interlock, and each RLock object instance can come from a different Redisson instance.

Of course, this is the introduction of the official website. What is it? Use interlock MultiLock and source code.

MultiLock use

According to the official documentation, the Redisson client may not be the same. Of course, the general work does not say not to use a client.

lock

Before reading MultiLock, you should have read about normal locking.

Source: entrance org. Redisson. RedissonMultiLock# lock ()

The default timeout leaseTime is not set, so it is -1.

This method is too long. Let’s break it down and read it.

  1. BaseWaitTime = Number of locks * 1500, which in this case is 4500 milliseconds;
  2. leaseTime == -1So waitTime = baseWaitTime, which is 4500;
  3. while (true)Call tryLock to lock until successful.

Call the tryLock method with waitTime = 4500, leaseTime = -1, unit = MILLISECONDS.

What’s the logic behind tryLock?

leaseTime ! If lambda is equal to negative 1, I’m going to skip this part.

waitTime ! RemainTime = 4500, lockWaitTime = 4500

Therefore, failedLocksLimit() returns 0, which means all locks must have been successfully locked.

Here’s the point:

Iterate through all the locks and lock them in turn.

Locking logic is no different from reentrant locking. So Lua scripts don’t do the analysis.

This is the result of tryLock.

The successful lock is placed in the acquiredLocks collection.

The failedLocksLimit value is 0, and all locks in the set of acquiredLocks are released, the set of locks is cleared, and iterators are restored.

RemainTime, if remainTime is less than or equal to 0, then lock timeout, return false.

This will execute the external while (true) logic and then RedissonMultiLock#tryLock again.

Release the lock

After looking at the locking logic, lock release is easier to understand.

UnlockAsync () is the RedissonBaseLock#unlockAsync() method called.

conclusion

According to my understanding, the diagram is as follows:

Key1, key2, key3… The keyN is placed into a List, and the lock is iterated until all are successful. When unlocking, you iterate over the lock to release it.

Related to recommend

  • Redisson distributed lock source 07: Fair lock release
  • Redisson distributed lock source code 06: fair lock queuing lock
  • Redisson distributed lock source 05: fair lock lock