Interviewer: Is ReadWriteLock mutually exclusive between reading and writing?

 
  
 
  
您好,我是路人,更多优质文章见个人博客:http://itsoku.com


When encountering concurrency problems during development, locks are generally used. Synchronized has an obvious performance problem that is mutual exclusion between reading and reading;

ReadWriteLock is a read-write separation lock provided in JDK5. Read-write separation locks can effectively help reduce lock competition and improve system performance.

ReadWriteLock manages a set of locks, one is a read-only lock and the other is a write lock.

ReetrantReadWriteLock in the Java Concurrency Library implements the ReadWriteLock interface and adds reentrant features.

The read-write lock ReentrantReadWriteLock: read-read shared, read-write mutual exclusion, write-write mutual exclusion; read-write lock maintains a pair of locks, a read lock, a write lock, by separating the read lock and write lock, so that the concurrency is relatively The general exclusive lock has been greatly improved. In the case of more reads and fewer writes, read-write locks can provide better concurrency and throughput than exclusive locks.

04fcd9d2fc6a514d21fb7b913d638705.png

It can be seen from the source code that the read-write lock also relies on the queue synchronizer Sync (AQS) to realize the synchronization function, and the read-write status is the synchronization status of its synchronizer. Recommended: Java Advanced Video Resources

The following is an example to illustrate: read-read sharing, read-write mutual exclusion, and write-write mutual exclusion .

code show as below:

public class ReentrantWriteReadLockTest {
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    ReadLock readLock = lock.readLock();
    WriteLock writeLock = lock.writeLock();
    
    public void read(){
        try {
            readLock.lock();
            System.out.println("线程"+Thread.currentThread().getName()+"进入。。。");
            Thread.sleep(3000);
            System.out.println("线程"+Thread.currentThread().getName()+"退出。。。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            readLock.unlock();
        }
    }
    
    public void write(){
        try {
            writeLock.lock();
            System.out.println("线程"+Thread.currentThread().getName()+"进入。。。");
            Thread.sleep(3000);
            System.out.println("线程"+Thread.currentThread().getName()+"退出。。。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            writeLock.unlock();
        }
    }
    
 
    public static void main(String[] args) {
        final ReentrantWriteReadLockTest wr = new ReentrantWriteReadLockTest();
        Thread t1 = new Thread(new Runnable() {
            public void run() {
                wr.read();
            }
        }, "t1");
        Thread t2 = new Thread(new Runnable() {
            public void run() {
                wr.read();
            }
        }, "t2");
        Thread t3 = new Thread(new Runnable() {
            public void run() {
                wr.write();
            }
        }, "t3");
        Thread t4 = new Thread(new Runnable() {
            public void run() {
                wr.write();
            }
        }, "t4");
        
        t1.start();
        t2.start();
        //t3.start();
        //t4.start();
    }
}

When we start threads t1 and t2, the result is as follows:

f123d24c6644c316e65db378ceb73422.png

Threads t1 and t2 can enter at the same time, which explains it 读读共享!

When we start threads t2 and t3, the result is as follows:

6b7a750d08cf866696a51d8aa01c0c4f.png

A thread must wait for another thread to exit before it can enter, which explains it 读写互斥!

When we start threads t3 and t4, the result is as follows:

6d9951e64aee36269dae65c8d3cf7ea2.png

A thread must wait for another thread to exit before it can enter, which explains it 写写互斥!

more good articles

  1. Java High Concurrency Series (34 articles in total)

  2. MySql master series (27 articles in total)

  3. Maven master series (10 articles in total)

  4. Mybatis series (12 articles in total)

  5. Talk about common implementations of db and cache consistency

  6. Interface idempotence is so important, what is it? How to achieve it?

  7. Generics, a bit difficult, will make many people confused, that's because you didn't read this article!

↓↓↓ 点击阅读原文,直达个人博客
你在看吗

Guess you like

Origin blog.csdn.net/likun557/article/details/131971322