当前位置:网站首页>Concurrent operation -- reentrantreadwritelock
Concurrent operation -- reentrantreadwritelock
2022-06-09 03:25:00 【RobertTeacher】
Concurrent operations
Concurrent operation ——ReentrantReadWriteLock.
Concurrent operation ——ReentrantReadWriteLock
Concurrent operation ——ReentrantReadWriteLock
The problem of locking multiple threads in high concurrency environment .
One 、ReentrantReadWriteLock
1、 Read-write lock
Read write lock interface readWriteLock An implementation of the interface , Read and write separation is achieved .
2、 Fair and unfair lock
Support fair and unfair locks , So is the bottom floor AQS Realization .
3、 Lock down
Allow demotion from write lock to read lock
technological process : Get the write lock first , Then get the read lock , Finally, release the write lock ; But you can't upgrade from read lock to write lock .
4、 Reentrant
After reading the lock, you can also obtain the read lock , After obtaining a write lock, you can obtain a write lock and a read lock .
5、 The core
Read locks are shared , The write lock is exclusive . There is no mutual exclusion between reading and reading , Read and write 、 Writing and reading are mutually exclusive , It mainly improves the performance of reading and writing .
Two 、ReentrantReadWriteLock Application scenarios
Application scenarios : Read more and write less , For example, designing a cache component or Improve Collection The concurrency of
The source code itself provides cases , Example 1 : use ReenTrantReadWriteLock Do the read / write lock separation operation
class CachedData {
Object data;
volatile boolean cacheValid;
final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
void processCachedData() {
rwl.readLock().lock();
if (!cacheValid) {
// Must release read lock before acquiring write lock
rwl.readLock().unlock();
rwl.writeLock().lock();
try {
// Recheck state because another thread might have
// acquired write lock and changed state before we did.
if (!cacheValid) {
data = ...
cacheValid = true;
}
// Downgrade by acquiring read lock before releasing write lock
rwl.readLock().lock();
} finally {
rwl.writeLock().unlock(); // Unlock write, still hold read
}
}
try {
use(data);
} finally {
rwl.readLock().unlock();
}
}
}}
The source code itself provides cases , Example 2 : Implementation of a thread safe map
class RWDictionary {
private final Map<String, Data> m = new TreeMap<String, Data>();
private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
private final Lock r = rwl.readLock();
private final Lock w = rwl.writeLock();
public Data get(String key) {
r.lock();
try {
return m.get(key); }
finally {
r.unlock(); }
}
public String[] allKeys() {
r.lock();
try {
return m.keySet().toArray(); }
finally {
r.unlock(); }
}
public Data put(String key, Data value) {
w.lock();
try {
return m.put(key, value); }
finally {
w.unlock(); }
}
public void clear() {
w.lock();
try {
m.clear(); }
finally {
w.unlock(); }
}
}}
3、 ... and 、ReentrantReadWriteLock and ReenTrantLock The difference between

summary
The main difference is ReenTrantReadWriteLock contrast ReenTrantLock Read / write lock separation can be realized , Then there ReenTrantReadWriteLock Special reentry locking mechanism .
边栏推荐
- Two merged sequences (CF 1144 g) -dp
- 關於JS console.log() 是同步 or 异步引發的問題
- 富士康又遭勒索攻击,大量未加密文件泄露
- Zhongang Mining: fluorite resources listed in the strategic mineral catalogue
- Ccf-csp 201412-3 call auction
- STM32 flash erase crash
- Laravel determines whether the mailbox already exists and verifies whether the mailbox format is legal
- Do you know websocket?
- Tamidog information | Maersk completed another large-scale enterprise acquisition
- Is CICC wealth safe? I want to open an account
猜你喜欢

RTSP/Onvif协议视频平台EasyNVR如何配置用户的视频流播放时长?
Ccf-csp 202012-3 file system with quota

Ccf-csp 201909-4 recommended system 100 points

Ccf-csp 202203-3 computing resource scheduler 80 points

Runtime constant area - Method area

Leetcode 974. And K divisible subarray prefix sum

并发操作之——ReentrantReadWriteLock

What taxes do Tami dogs need to pay for equity transfer? What are the possible tax risks?

Do you know websocket?

ERP总体介绍
随机推荐
Binding mode of QT overloaded signal slot function
Date tool class - conversion of operation string to date and localdate, time difference between two dates, etc
Generation of random numbers in C language [detailed explanation]
mongodb数据库文档显示异常
opencv学习笔记一
Ccf-csp 201903-4 messaging interface
Ccf-csp 201803-5 quadratic summation 30 points
SQL审核 | 这里有 MySQL/Oracle 最常用的 SQL 开发规则
Foxconn suffered another blackmail attack, and a large number of unencrypted files were leaked
"Baget" takes you one minute to set up your own private nuget server
Handwriting perceptron, KNN, decision tree (ID3) for binary classification of iris
Leetcode 1310. Subarray XOR query prefix and + XOR
Neural network learning (IV) -- a simple summary of the knowledge of each layer of neural network
Ccf-csp 202109-3 pulse neural network, sometimes 100 points..
并发操作之——ReenTrantLock和synchronized的区别
QT project add compile error reporting option
Do you know the specifications of e-commerce background permission settings!
This book has won the 2022 Beijing college entrance examination composition!
故障分析 | 大量短时进程导致 cpu 负载过高案例一则
Two merged sequences (CF 1144 g) -dp