Watchdog mechanism is in RedissonBaseLock# scheduleExpirationRenewal method, the fair’s locked and not fair and no difference.

As you saw earlier, when a fair lock fails, the current is placed in a waiting queue, repeatedly trying to obtain the lock through a loop in your Java code.

Release the lock

Take the initiative to release

Source: RedissonFairLock# unlockInnerAsync

  1. Keys [1] : name of lock,anyLock;
  2. Keys [2] : Locked wait queue,redisson_lock_queue:{anyLock};
  3. Keys [3] : Set of thread locking time in wait queueredisson_lock_timeout:{anyLock}Is stored in the collection according to the timestamp of the lock;
  4. KEYS [4] :redisson_lock__channel:{anyLock};
  5. ARGV [1] : LockPubSub. UNLOCK_MESSAGE;
  6. ARGV[2] : lock timeout time 30000;
  7. ARGV[3] : UUID: threaId combination58f6c4a2-9908-4957-b229-283a45359c4b:47;
  8. ARGV[4] : CurrentTime Current timestamp

This logical highlight has been highlighted, and the point is to release the lock.

  1. If the lock is in the queue, it will be removed from the queue if it timeouts.
  2. If the number of reentrances is greater than 0, the timeout is reset. If it is not greater than 0, the lock is removed.

In this case, other threads subsequently acquire the lock from the waiting queue.

Timeout to delete

In a Lua script that locks and releases a lock, the first paragraph is always a while true do XXX, which is used to remove a lock from the queue that has timed out.

The release of the thread holding the lock is no different from that of an unfair lock, which is automatically released when the lock times out or when the service is down. (This refers to anyLock).


The release of fair locks is also divided into active release and timeout release.

  1. Active release, that is, call the release lock.
  2. Timeout deletion, is divided into two kinds, one is the thread holding the lock timeout deletion, this kind and non-fair lock has no difference, because this lock also contains the timeout time + watchdog renewal lease. The other is to delete the timeout in the waiting queue, which is to determine whether the timestamp of the first waiting thread has timeout before each lock is acquired, so as to remove the lock.

Related to recommend

  • Redisson distributed lock source code 06: fair lock queue lock
  • Redisson distributed lock source code 05: fair lock lock
  • Redisson distributed lock source code 04: Reentrant lock release