当前位置:网站首页>Concurrent operation -- reentrantreadwritelock

Concurrent operation -- reentrantreadwritelock

2022-06-09 03:25:00 RobertTeacher

Concurrent operations

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 .
 Insert picture description here

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 .
 Insert picture description here

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

 Insert picture description here


summary

The main difference is ReenTrantReadWriteLock contrast ReenTrantLock Read / write lock separation can be realized , Then there ReenTrantReadWriteLock Special reentry locking mechanism .

原网站

版权声明
本文为[RobertTeacher]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090323358167.html