当前位置:网站首页>Redistribution distributed lock types
Redistribution distributed lock types
2022-07-24 10:54:00 【dotaer-df】
be based on Redis Of Redisson The distributed reentrant lock realizes
java.util.concurrent.locks.LockInterface
Redisson The types of locks are as follows : Reentrant lock , Fair lock , interlocking , Red lock , Read-write lock , Next, we will introduce each lock in detail .
- Reentrant lock
A thread is executing a method with a lock , This method calls another method that needs the same lock , Then the thread can directly execute the called method , Without having to reacquire the lock .
public void method1() { RLock lock = redissonClient.getLock("lock"); try { lock.lock(); method2(); } finally { lock.unlock(); } System.out.println(" Lock release successful "); } public void method2() { RLock lock = redissonClient.getLock("lock"); try { if (lock.tryLock()) { System.out.println(" Locking success "); // Business logic } } finally { lock.unlock(); } } - Fair lock
It's guaranteed to be multiple Redisson When a client line requests a lock at the same time , Priority is given to the thread that made the request first . All request threads will be queued in a queue , When a thread goes down ,Redisson Will wait for 5 Seconds to continue to the next thread , That is to say, if there is 5 All threads are waiting , Then the next thread will wait for at least 25 second .
RLock fairLock = redisson.getFairLock("anyLock"); // The most common use method fairLock.lock(); - interlocking
When you can associate multiple lock objects with one lock object
RLock lock1 = redissonInstance1.getLock("lock1"); RLock lock2 = redissonInstance2.getLock("lock2"); RLock lock3 = redissonInstance3.getLock("lock3"); RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3); // At the same time lock :lock1 lock2 lock3 // Success is only when all the locks are locked . lock.lock(); // Business logic lock.unlock();- Red lock
When there are most ( More than half ) After locking successfully , To really get the lock
RLock lock1 = redissonInstance1.getLock("lock1");
RLock lock2 = redissonInstance2.getLock("lock2");
RLock lock3 = redissonInstance3.getLock("lock3");
RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3);
// At the same time lock :lock1 lock2 lock3
// A red lock is successful if it locks most nodes . Here you are 3 A lock , At least 2 One lock succeeded , It's really successful to lock
lock.lock();
...
lock.unlock();- Read-write lock
When reading a lock, multiple threads can obtain , Write lock can only be obtained by one thread , After testing, when the write lock has not been released , Read lock blocking cannot be obtained , Until the write lock is released .
/**
* Make sure you can read the latest data , During revision , Writing lock is an exclusive lock ( The mutex ). Read lock is a shared lock
* The lock is not released , You have to wait to read
*/
@GetMapping("/read")
@ResponseBody
public String readValue() {
RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
RLock rLock = readWriteLock.readLock();
try {
rLock.lock();
} catch (Exception e) {
e.printStackTrace();
} finally {
rLock.unlock();
}
return "";
}
@GetMapping("/write")
@ResponseBody
public String writeValue() {
RReadWriteLock readWriteLock = redisson.getReadWriteLock("rw-lock");
RLock rLock = readWriteLock.writeLock();
try {
//1、 Change data and add write lock , Read data and add read lock
rLock.lock();
TimeUnit.SECONDS.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
rLock.unlock();
}
return "";
}
边栏推荐
- 563 pages (300000 words) overall design scheme of smart Chemical Park (phase I)
- Take care of me when I meet you for the first time
- MySQL - 唯一索引
- 机器学习小试(11)验证码识别测试-使用Qt与Tensorflow2进行深度学习实验
- Sentinel three flow control modes
- Zero basic learning canoe panel (4) -- button
- MySQL - full text index
- [about Modelsim simulation] design and Simulation of 4-bit counter
- Detailed explanation of Flink operation architecture
- 基于NoCode构建简历编辑器
猜你喜欢
![[FPGA]: IP core ibert](/img/f9/ef4c8d44be2e27b6d85010ca8cdefa.png)
[FPGA]: IP core ibert

Flink 运行架构详解

Zero basic learning canoe panel (3) -- static text, group box, picture box

Machine learning quiz (10) using QT and tensorflow to create cnn/fnn test environment

Sentinel three flow control modes

向量化引擎对HTAP的价值与技术思考

MySQL - normal index

Zero basic learning canoe panel (7) -- file selection (pathdiaglog)

How to build a node development environment efficiently

Zero basic learning canoe panel (8) -- hex/text editor
随机推荐
[FPGA]: IP core ibert
MySQL - unique index
UVM - two way communication
变频器的工作原理和功能应用
零基础学习CANoe Panel(10)—— 复选框(CheckBox)
Is it safe to open an online stock account?
二叉树基础知识概览
MySQL - 普通索引
[class, abstraction and inheritance]
Zero basic learning canoe panel (9) -- combobox
Machine learning quiz (10) using QT and tensorflow to create cnn/fnn test environment
Disk storage chain B-tree and b+ tree
分布式事务处理方案大 PK!
[interview: Basics 04: insert order]
Rtklib source code, RTK difference calculation, rtkpos and replos function process sorting
Real time weather API
Cookie sessionstorage localstorage differences
QT create application tray and related functions
[interview: Basics 01: integer binary search]
Arduino + AD9833 waveform generator