当前位置:网站首页>Reentrant read write lock feature summary of reentrantreadwritelock
Reentrant read write lock feature summary of reentrantreadwritelock
2022-07-22 04:12:00 【jwolf2】
Re enter the read-write lock ReentrantReadWriteLock Feature summary
- Reentrant : Because it's a re entrant lock , And in the same thread , Therefore, the lock can be obtained repeatedly , Ordinary Lock Even if it is the same thread, it cannot get repeatedly , It's not reentrant ; You have to release as many times as you re-enter .
- Mutex and lock up degradation : Reading is not mutually exclusive , Reading and writing & Writing mutually exclusive ;;; Rules are not applicable in the same thread , But in the same thread, such as this example, the case of lock degradation is OK Of , But if you read the lock first and then add a write lock, it is a lock upgrade , Deadlock will occur
- Fairness :ReentrantReadWriteLock Declare as true Fair lock , namely “ Thread waiting for lock sorting ” And “ The thread finally gets the lock sort ” It's consistent , Otherwise seize
- Solutions in distributed environment redission Distributed environment readWriteLock
public class App {
private static final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(true);
public static void main(String[] args) throws Exception {
Thread t = new Thread(() -> {
readWriteLock.writeLock().lock();
System.out.println("writeLock1");
readWriteLock.writeLock().lock();// Because it's a re entrant lock , And in the same thread , Therefore, the lock can be obtained repeatedly , Ordinary Lock Even if it is the same thread, it cannot get repeatedly ;
System.out.println("writeLock1");
readWriteLock.readLock().lock(); // Different threads : Reading is not mutually exclusive , Reading and writing & Writing mutually exclusive ;;; Rules are not applicable in the same thread , But in the same thread, such as this example, the case of lock degradation is OK Of , But if you read the lock first and then add a write lock, it is a lock upgrade , Deadlock will occur
System.out.println("readLock1");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
readWriteLock.writeLock().unlock();
readWriteLock.writeLock().unlock();// The front is locked twice , So it needs to be released twice
readWriteLock.readLock().unlock();
});
t.start();
Thread.sleep(1000);
// start-up N Threads : Test fair lock 、 Not fair lock
for (int i = 0; i < 100; i++) {
int finalI = i;
new Thread(() -> {
System.out.println(" Thread waiting for lock sorting "+finalI);
// Because the write lock has been t Thread acquisition ,t The write lock obtained by the thread is sorted by the read lock here , So it is blocked here .5s after t Thread release lock , Because of the ReentrantReadWriteLock Declare as true Fair lock , namely “ Thread waiting for lock sorting ” And “ The thread finally gets the lock sort ” It's consistent
readWriteLock.readLock().lock();
System.out.println(" The thread finally gets the lock sort "+ finalI);
}).start();
}
}
}
边栏推荐
猜你喜欢
814. 二叉树剪枝 : 简单递归运用题
Implementing DDD based on ABP -- domain service, application service and dto practice
Thesis reading: dialogpt
Xlnet: operation mechanism and comparison with Bert -- Summary
A tool that I hate to meet later, utools
Yan Weimin Chapter II after class exercises (2.29-2.38)
[CCF CSP] 201903-1 small, medium and large
Implementing DDD based on ABP -- domain service, application service and dto practice
合并二叉树
Shell programming - functions
随机推荐
树模型总结
5.2 access control
How to install Google play store in Xiaomi 10
[CCF CSP] 201512-1 sum of digits
Judge whether the binary tree is symmetric
编译php7 指定remi扩展目录和ini的配置目录
[CCF CSP] 2012-1 Xiao Ming goes to school
知识蒸馏是什么?一份入门随笔__摘要
[CCF CSP] 201809-1 selling vegetables
XLNet学习:究极总结
【CCF CSP】201403-1相反数
Shenniu tt685c flash ETTL mode asynchronous solution
3 pairs of easy to mix APIs for linkedblockingqueue
gadget驱动框架(六)
如何解开和反编译思科cisco的交换机固件
gadget之udc
一款令我相见恨晚的工具Utools
【CCF CSP】201809-1卖菜
Determine whether binary search tree
【CCF CSP】201703-1分蛋糕