Home > Database > Redis > What is a reentrant lock? Detailed explanation of how redis implements distributed reentrancy locks

What is a reentrant lock? Detailed explanation of how redis implements distributed reentrancy locks

青灯夜游
Release: 2022-02-17 10:47:38
forward
9028 people have browsed it

What is a reentrant lock? How to implement reentrancy lock? The following article will give you an in-depth discussion on how redis implements distributed reentrancy locks. I hope it will be helpful to you!

What is a reentrant lock? Detailed explanation of how redis implements distributed reentrancy locks

What is a non-reentrant lock?

That is, if the current thread executes a method and has acquired the lock, then when trying to acquire the lock again in the method, it will not be able to acquire it and will be blocked.

What is a reentrant lock?

Reentrant lock, also called recursive lock, means that in the same thread, after the outer function obtains the lock, the inner recursive function can still obtain the lock. That is, when the same thread enters the same code again, it can get the lock again.

What is the function of reentrant lock?

Prevent deadlock from acquiring the lock multiple times in the same thread.

Note: In java programming, synchronized and ReentrantLock are both reentrant locks.

Reentrant lock based on synchronized

Step 1: Double locking logic

public class SynchronizedDemo {
    //模拟库存100
    int count=100;
    public synchronized void operation(){
        log.info("第一层锁:减库存");
        //模拟减库存
        count--;
        add();
        log.info("下订单结束库存剩余:{}",count);
    }

    private synchronized void add(){
        log.info("第二层锁:插入订单");
        try {
            Thread.sleep(1000*10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Copy after login

Step 2: Add a test class

public static void main(String[] args) {
    SynchronizedDemo synchronizedDemo=new SynchronizedDemo();
    for (int i = 0; i < 3; i++) {
        int finalI = i;
        new Thread(()->{
            log.info("-------用户{}开始下单--------", finalI);
            synchronizedDemo.operation();
        }).start();
    }
}
Copy after login

Step 3: Test

20:44:04.013 [Thread-2] INFO com.agan.redis.controller.SynchronizedController - -------用户2开始下单--------
20:44:04.013 [Thread-1] INFO com.agan.redis.controller.SynchronizedController - -------用户1开始下单--------
20:44:04.013 [Thread-0] INFO com.agan.redis.controller.SynchronizedController - -------用户0开始下单--------
20:44:04.016 [Thread-2] INFO com.agan.redis.Reentrant.SynchronizedDemo - 第一层锁:减库存
20:44:04.016 [Thread-2] INFO com.agan.redis.Reentrant.SynchronizedDemo - 第二层锁:插入订单
20:44:14.017 [Thread-2] INFO com.agan.redis.Reentrant.SynchronizedDemo - 下订单结束库存剩余:99
20:44:14.017 [Thread-0] INFO com.agan.redis.Reentrant.SynchronizedDemo - 第一层锁:减库存
20:44:14.017 [Thread-0] INFO com.agan.redis.Reentrant.SynchronizedDemo - 第二层锁:插入订单
20:44:24.017 [Thread-0] INFO com.agan.redis.Reentrant.SynchronizedDemo - 下订单结束库存剩余:98
20:44:24.017 [Thread-1] INFO com.agan.redis.Reentrant.SynchronizedDemo - 第一层锁:减库存
20:44:24.017 [Thread-1] INFO com.agan.redis.Reentrant.SynchronizedDemo - 第二层锁:插入订单
20:44:34.017 [Thread-1] INFO com.agan.redis.Reentrant.SynchronizedDemo - 下订单结束库存剩余:97
Copy after login
  • Since the synchronized keyword modifies methods, all locks are instance objects: synchronizedDemo
  • The running results show that each thread must run the two methods completely before the lock can be released. Only other threads can get the lock, that is, a thread can get the same lock multiple times and can be reentrant. So synchronized is also a reentrant lock.

Reentrant lock based on ReentrantLock

ReentrantLock is a reentrant and exclusive lock, and a recursive non-blocking synchronization lock. Compared with the synchronized keyword, it is more flexible and powerful, adding polling, timeout, interrupt and other advanced functions.

Step 1: Double locking logic

public class ReentrantLockDemo {

    private Lock lock =  new ReentrantLock();

    public void doSomething(int n){
        try{
            //进入递归第一件事:加锁
            lock.lock();
            log.info("--------递归{}次--------",n);
            if(n<=2){
                try {
                    Thread.sleep(1000*2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                this.doSomething(++n);
            }else{
                return;
            }
        }finally {
            lock.unlock();
        }
    }

}
Copy after login

Step 2: Add a test class

public static void main(String[] args) {
    ReentrantLockDemo reentrantLockDemo=new ReentrantLockDemo();
    for (int i = 0; i < 3; i++) {
        int finalI = i;
        new Thread(()->{
            log.info("-------用户{}开始下单--------", finalI);
            reentrantLockDemo.doSomething(1);
        }).start();
    }
}
Copy after login

Step 3: Test

20:55:23.533 [Thread-1] INFO com.agan.redis.controller.ReentrantController - -------用户1开始下单--------
20:55:23.533 [Thread-2] INFO com.agan.redis.controller.ReentrantController - -------用户2开始下单--------
20:55:23.533 [Thread-0] INFO com.agan.redis.controller.ReentrantController - -------用户0开始下单--------
20:55:23.536 [Thread-1] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归1次--------
20:55:25.537 [Thread-1] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归2次--------
20:55:27.538 [Thread-1] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归3次--------
20:55:27.538 [Thread-2] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归1次--------
20:55:29.538 [Thread-2] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归2次--------
20:55:31.539 [Thread-2] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归3次--------
20:55:31.539 [Thread-0] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归1次--------
20:55:33.539 [Thread-0] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归2次--------
20:55:35.540 [Thread-0] INFO com.agan.redis.Reentrant.ReentrantLockDemo - --------递归3次--------
Copy after login
  • The running results show that each thread can be locked and unlocked multiple times Yes, ReentrantLock is reentrant.

How does redis implement distributed reentrancy locks?

Although setnx can implement distributed locks, it is not reentrant. In some complex business scenarios, when we need distributed reentrant locks, There are still many solutions for the reentrancy lock of redis in the industry, and the most popular one currently is to use Redisson. [Related recommendations: Redis Video Tutorial]

What is Redisson?

  • #Redisson is the Java version of Redis client officially recommended by Redis.
  • Based on the common interfaces in the Java utility toolkit, it provides users with a series of commonly used tool classes with distributed characteristics.
  • In terms of network communication, it is based on NIO's Netty framework to ensure high performance of network communication.
  • In terms of the function of distributed locks, it provides a series of distributed locks; such as:
    • Reentrant Lock
    • Fair Lock Lock)
    • unFair Lock(unFair Lock)
    • ReadWriteLock(ReadWriteLock)
    • Interlock(MultiLock)
    • Red Lock(RedLock)

Case Combat: Experience redis distributed reentrancy lock

Step 1: Redisson configuration

Redisson configuration can be checked: redis distributed cache (34)-SpringBoot integrates Redission - Nuggets (juejin.cn)

https:// juejin.cn/post/7057132897819426824

Step 2: Redisson re-entry lock test class

public class RedisController {

    @Autowired
    RedissonClient redissonClient;

    @GetMapping(value = "/lock")
    public void get(String key) throws InterruptedException {
        this.getLock(key, 1);
    }

    private void getLock(String key, int n) throws InterruptedException {
        //模拟递归,3次递归后退出
        if (n > 3) {
            return;
        }
        //步骤1:获取一个分布式可重入锁RLock
        //分布式可重入锁RLock :实现了java.util.concurrent.locks.Lock接口,同时还支持自动过期解锁。
        RLock lock = redissonClient.getLock(key);
        //步骤2:尝试拿锁
        // 1. 默认的拿锁
        //lock.tryLock();
        // 2. 支持过期解锁功能,10秒钟以后过期自动解锁, 无需调用unlock方法手动解锁
        //lock.tryLock(10, TimeUnit.SECONDS);
        // 3. 尝试加锁,最多等待3秒,上锁以后10秒后过期自动解锁
        // lock.tryLock(3, 10, TimeUnit.SECONDS);
        boolean bs = lock.tryLock(3, 10, TimeUnit.SECONDS);
        if (bs) {
            try {
                // 业务代码
                log.info("线程{}业务逻辑处理: {},递归{}" ,Thread.currentThread().getName(), key,n);
                //模拟处理业务
                Thread.sleep(1000 * 5);
                //模拟进入递归
                this.getLock(key, ++n);
            } catch (Exception e) {
                log.error(e.getLocalizedMessage());
            } finally {
                //步骤3:解锁
                lock.unlock();
                log.info("线程{}解锁退出",Thread.currentThread().getName());
            }
        } else {
            log.info("线程{}未取得锁",Thread.currentThread().getName());
        }
    }
}
Copy after login

RLock three locking actions:

    1. Default lock
    • lock.tryLock();
    1. Supports expired unlocking function, automatically unlocked after 10 seconds after expiration
    • lock.tryLock(10, TimeUnit.SECONDS);
    1. Try to lock, wait up to 3 seconds, and automatically unlock after expiration 10 seconds after locking
    • lock.tryLock(3, 10, TimeUnit.SECONDS);

the difference:

  • lock.lock():阻塞式等待。默认加的锁都是30s
    • 锁的自动续期,如果业务超长,运行期间自动锁上新的30s。不用担心业务时间长而导致锁自动过期被删掉(默认续期)
    • 加锁的业务只要运行完成,就不会给当前锁续期,即使不手动解锁,锁默认会在30s内自动过期,不会产生死锁问题
    • lock()如果我们未指定锁的超时时间,就使用【看门狗默认时间】: lockWatchdogTimeout = 30 * 1000
    • 原理:只要占锁成功,就会启动一个定时任务【重新给锁设置过期时间,新的过期时间就是看门狗的默认时间】,每隔10秒都会自动的再次续期,续成30秒
  • lock.lock(10,TimeUnit.SECONDS) :10秒钟自动解锁,自动解锁时间一定要大于业务执行时间
    • 出现的问题:在锁时间到了以后,不会自动续期
    • 原理:lock(10,TimeUnit.SECONDS)如果我们传递了锁的超时时间,就发送给redis执行脚本,进行占锁,默认超时就是我们制定的时间

最佳实战:

  • lock.lock(10,TimeUnit.SECONDS); 省掉看门狗续期操作,自动解锁时间一定要大于业务执行时间,手动解锁

步骤3:测试

访问3次:http://127.0.0.1:9090/lock?key=ljw

线程http-nio-9090-exec-1业务逻辑处理: ljw,递归1
线程http-nio-9090-exec-2未取得锁
线程http-nio-9090-exec-1业务逻辑处理: ljw,递归2
线程http-nio-9090-exec-3未取得锁
线程http-nio-9090-exec-1业务逻辑处理: ljw,递归3
线程http-nio-9090-exec-1解锁退出
线程http-nio-9090-exec-1解锁退出
线程http-nio-9090-exec-1解锁退出
Copy after login

通过测试结果:

  • nio-9090-exec-1线程,在getLock方法递归了3次,即证明了lock.tryLock是可重入锁
  • 只有当nio-9090-exec-1线程执行完后,io-9090-exec-2 nio-9090-exec-3 未取得锁 因为lock.tryLock(3, 10, TimeUnit.SECONDS),尝试加锁,最多等待3秒,上锁以后10秒后过期自动解锁 所以等了3秒都等不到,就放弃了

总结

上面介绍了分布式重入锁的相关知识,证明了Redisson工具能实现了可重入锁的功能。其实Redisson工具包中还包含了读写锁(ReadWriteLock)和 红锁(RedLock)等相关功能,我们下篇文章再详细研究。

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of What is a reentrant lock? Detailed explanation of how redis implements distributed reentrancy locks. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template