当前位置:网站首页>ReentrantReadWriteLock Read-write lock
ReentrantReadWriteLock Read-write lock
2022-08-07 12:51:00 【monsoon extinction season】
用途:
用于读多写少的场景.
特点:
- A thread has acquired a write lock,Then the current thread is excluded before the lock is released,No other thread can acquire a read or write lock.But the current thread can acquire the read lock(降级)和写锁(重入).
- A thread has acquired a read lock,All threads can only acquire read locks before the locks are released(The current thread can re-entrant the read lock again),The write lock could not be acquired(That is, lock escalation is not supported).
- 读锁不支持条件变量,Throws an exception if used.
读写锁的使用
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
private Lock r = readWriteLock.readLock();
private Lock w = readWriteLock.writeLock(); // 读操作上读锁
public Data get(String key) {
r.lock();
try {
// TODO 业务逻辑
} finally {
r.unlock();
}
}
// 写操作上写锁
public Data put(String key, Data value) {
w.lock();
try {
// TODO 业务逻辑
} finally {
w.unlock();
}
}锁降级
读写状态的设计
- 写状态,等于 S & 0x0000FFFF(将高 16 位全部抹去). 当写状态加1,等于S+1.
- 读状态,等于 S >>> 16 (无符号补 0 右移 16 位).当读状态加1,等于S+(1<<16),也就是S+0x00010000根据状态的划分能得出一个推论:S不等于0时,当写状态(S&0x0000FFFF)等于0时,则读状态(S>>>16)大于0,即读锁已被获取.

写锁的获取
- 读写互斥
- 写写互斥
- 写锁支持同一个线程重入
- writerShouldBlock写锁是否阻塞实现取决公平与非公平的策略(FairSync和 NonfairSync)

读锁的获取
- 读锁共享,读读不互斥
- 读锁可重入,每个获取读锁的线程都会记录对应的重入数
- 读写互斥,锁降级场景除外
- 支持锁降级,持有写锁的线程,可以获取读锁,但是后续要记得把读锁和写锁读释放
- readerShouldBlock读锁是否阻塞实现取决公平与非公平的策略FairSync和NonfairSync)


边栏推荐
- China University of Petroleum (Beijing) - "Workover Engineering" Phase II Online Work
- China University of Petroleum (Beijing) - Online Operation of "Reservoir Engineering" Phase II
- Custom types: structs, enums, unions
- USB模块分析(四)- 设备列表&权限申请
- R 中因子添加、修改和删除
- ‘utf-8‘ codec can‘t decode byte 0xb9 in position 0: invalid start byte
- [js] Calculate the distance from the child node to the parent node and scroll to the position
- Prometheus 自定义metrics
- Character (string) and memory manipulation library functions
- Weekend resumption, masters do not sell products but tell stories!
猜你喜欢

MySQL - usage of case when, longitudinal statistics - similar to pivot table

自定义类型:结构体,枚举,联合
![[Deep Learning] Li Mu's Deep Learning Notes are here!](/img/68/4e032f65a06dc31abc491a99de9dda.png)
[Deep Learning] Li Mu's Deep Learning Notes are here!

What is the Office Open XML file format

VMware's record of virtual machine expansion

PDF conversion tool installation tutorial

若依集成just-auth实现第三方授权登录

Campus basketball web finished goods movement series of NBA basketball theme school basketball template pages simple sports website design product

免费的编程连字等宽字体:Fira Code

1. MongoDB概览
随机推荐
VMware's record of virtual machine expansion
ESP8266-Arduino编程实例-L3GD20三轴角速率传感器驱动
rpc.rquotad: unable to register (RQUOTAPROG, RQUOTAVERS, udp)
LeetCode高频题:最长公共子序列,玩游戏A和游戏B,两兄弟加起来最多可以获得多少奖品
FairMOT在线跟踪框架及代码学习记录
微信小程序和微信H5有什么区别?
MySQL - usage of case when, longitudinal statistics - similar to pivot table
eyb:创建过程:搭建环境到显示菜单(一)
7.cuBLAS开发指南中文版--cuBLAS中的cublasSetVector()和cublasGetVector()
Realize database addition, deletion, modification and query in one page
China university of petroleum (Beijing) - "completion project" the second phase of online operations
周末复盘,高手不卖产品只讲故事!
[js] Calculate the distance from the child node to the parent node and scroll to the position
dll repair tool installation tutorial
eyb: Creation process: build the environment to display the menu (1)
自定义类型:结构体,枚举,联合
MySQL 报错原因:ERROR 1241 (21000): Operand should contain 1 column(s)
若依集成just-auth实现第三方授权登录
What is the Office Open XML file format
中国石油大学(北京)-《 完井工程》第二阶段在线作业