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!
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.
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.
Prevent deadlock from acquiring the lock multiple times in the same thread.
Note: In java programming, synchronized and ReentrantLock are both reentrant locks.
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(); } } }
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(); } }
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
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(); } } }
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(); } }
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次--------
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?
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()); } } }
RLock three locking actions:
lock.tryLock(3, 10, TimeUnit.SECONDS);
the difference:
最佳实战:
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解锁退出
通过测试结果:
上面介绍了分布式重入锁的相关知识,证明了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!