Java并发编程之读写锁ReentrantReadWriteLock

简介

ReentrantReadWriteLock:可重入读写锁,它提供了获取读锁和写锁的方法,读锁可以并发执行,但是写锁只能一个线程调用其他线程等待。

示例1 读锁并发

    private static final ReadWriteLock lock = new ReentrantReadWriteLock();
    private static final AtomicInteger ai = new AtomicInteger(1);

    public static void demo01() {
        Thread t1 = new Thread(ReentrantReadWriteLockDemo::read, "t1");
        Thread t2 = new Thread(ReentrantReadWriteLockDemo::read, "t2");
        Thread t3 = new Thread(ReentrantReadWriteLockDemo::read, "t3");

        t1.start();
        t2.start();
        t3.start();
    }

    private static void read() {
        String thName = Thread.currentThread().getName();
        try {
            System.out.println("线程:" + thName + " 准备就绪");
            Thread.sleep(1000);
            lock.readLock().lock();
            System.out.println("线程:" + thName + " 持有读锁");
            for (int i = 0; i < 10; i++) {
                System.out.println("线程:" + thName + " 正在进行读操作 ==> " + ai.getAndIncrement());
                Thread.sleep(100);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.readLock().unlock();
            System.out.println("线程:" + thName + " 释放读锁");
        }
    }

Java并发编程之读写锁ReentrantReadWriteLock_第1张图片

示例2 写锁独占

    public static void demo02() {
        Thread t1 = new Thread(ReentrantReadWriteLockDemo::write, "t1");
        Thread t2 = new Thread(ReentrantReadWriteLockDemo::write, "t2");
        Thread t3 = new Thread(ReentrantReadWriteLockDemo::write, "t3");

        t1.start();
        t2.start();
        t3.start();
    }

    private static void write() {
        String thName = Thread.currentThread().getName();
        try {
            System.out.println("线程:" + thName + " 准备就绪");
            Thread.sleep(1000);
            lock.writeLock().lock();
            System.out.println("线程:" + thName + " 持有写锁");
            for (int i = 0; i < 10; i++) {
                System.out.println("线程:" + thName + " 正在进行写操作 ==> " + ai.getAndIncrement());
                Thread.sleep(100);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.writeLock().unlock();
            System.out.println("线程:" + thName + " 释放写锁");
        }
    }

Java并发编程之读写锁ReentrantReadWriteLock_第2张图片

示例3 持有读锁,再申请写锁需等待读锁释放

    public static void demo03() throws InterruptedException {
        Thread t1 = new Thread(ReentrantReadWriteLockDemo::read, "t1");
        Thread t2 = new Thread(ReentrantReadWriteLockDemo::write, "t2");
        Thread t3 = new Thread(ReentrantReadWriteLockDemo::read, "t3");

        t1.start();
        Thread.sleep(100);
        t2.start();
        t3.start();
    }

Java并发编程之读写锁ReentrantReadWriteLock_第3张图片

示例4 持有写锁,再申请读锁或写锁都需要等待写锁释放

    private static void demo04() throws InterruptedException {
        Thread t1 = new Thread(ReentrantReadWriteLockDemo::write, "t1");
        Thread t2 = new Thread(ReentrantReadWriteLockDemo::read, "t2");
        Thread t3 = new Thread(ReentrantReadWriteLockDemo::write, "t3");

        t1.start();
        Thread.sleep(100);
        t2.start();
        t3.start();
    }

Java并发编程之读写锁ReentrantReadWriteLock_第4张图片

你可能感兴趣的:(多线程,并发编程,多线程,并发编程)