JUC之读写锁reentrantReadWriteLock详解

读写锁ReadWriteLock

一:什么是读写锁

读写锁是jdk5提供的读写分离锁,通过分离读和写来减少锁之间的竞争。举个简单例子,线程A、B负责写操作,线程C、D负责读操作。这样一来,可以使得C、D线程真正并行,保证了读可以同时读。但是读和写之间还是要需要相互等待以及持有锁的。它们之间的关系如下:

非阻塞 阻塞
阻塞 阻塞
  • 读-读:不阻塞
  • 读-写:阻塞
  • 写-写:阻塞

二:实战

public class ReadWriteLockDemo {
    private static Lock lock = new ReentrantLock();
    private static ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
    private static Lock readLock = reentrantReadWriteLock.readLock();
    private static Lock writeLock = reentrantReadWriteLock.writeLock();
    private int value;

    public Object handleRead(Lock lock) throws InterruptedException {
        try {
            lock.lock();
            Thread.sleep(1000);
            System.out.println("读取了:" + value);
            return value;
        } finally {
            lock.unlock();
        }
    }

    public void handleWrite(Lock lock, int index) throws InterruptedException {
        try {
            lock.lock();
            Thread.sleep(1000);
            System.out.println("写入了:" + value);
            value = index;
        } finally {
            lock.unlock();
        }
    }

    public static void main(String args[]) {
        final ReadWriteLockDemo demo = new ReadWriteLockDemo();

        Runnable readRunnable = new Runnable() {
            @Override
            public void run() {
                               try {
                    demo.handleRead(readLock);
                    //demo.handleRead(lock);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };

        Runnable writeRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    demo.handleWrite(writeLock, new Random().nextInt(100));
                    //demo.handleWrite(lock, new Random().nextInt(100));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };
        for (int i = 0; i < 18; i++) {
            new Thread(readRunnable).start();
        }
        for (int i = 18; i < 20; i++) {
            new Thread(writeRunnable).start();
        }
    }
}

首先让读和写都耗时1秒,然后通过demo.handleRead(readLock)和 demo.handleRead(readLock)进行读和写,读使用读锁,写使用写锁,,会发现读线程并行执行,而写操作会阻塞。大概就需要2秒执行完,如果你使用了重入锁,不好意思大概需要20秒。


你可能感兴趣的:(并发编程,读写锁)