在Redis分布式锁:redissonLock和RedLock底层都有加锁失败的场景处理,比如:A线程在加锁失败之后,会返回当前已经加锁的线程B还有多久到期,然后线程A就会休眠指定的时间,然后再进行加锁
这里自己在学习的时候,有一个点,一直没注意,就是Redis分布式锁在休眠的时候,是通过什么机制实现的?
在Redis分布式锁之:RedissonLock这篇博客中,有介绍加锁的具体流程,所以,这里就不再重复了,直接戳到核心代码里面来看
加锁失败的代码
@Override
public void lockInterruptibly(long leaseTime, TimeUnit unit) throws InterruptedException {
long threadId = Thread.currentThread().getId();
Long ttl = tryAcquire(leaseTime, unit, threadId);
// lock acquired
if (ttl == null) {
return;
}
RFuture<RedissonLockEntry> future = subscribe(threadId);
commandExecutor.syncSubscription(future);
try {
while (true) {
ttl = tryAcquire(leaseTime, unit, threadId);
// lock acquir