JAVA多线程进阶篇 3、JUC读写锁之ReadWriteLock


JUC 即 java.util.concurrent 包,提供了大量的工具类来简化并发编程。

ReadWriteLock 读写锁,在读时允许其他线程读不允许写,在写时不允许其他线程读和写。

通过读写锁对象分别取出读锁和写锁。

    ReadWriteLock rwlock = new ReentrantReadWriteLock();
    Lock rlock = rwlock.readLock();
    Lock wlock = rwlock.writeLock();

1.ReadWriteLock 提升了整体效率

package com.concurrent.juc.ch06;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReadWriteLockTest {
    ReadWriteLock rwlock = new ReentrantReadWriteLock();
    Lock rlock = rwlock.readLock();
    Lock wlock = rwlock.writeLock();

    String val = "读写锁";
    CountDownLatch latch = new CountDownLatch(120);

    void read() {
        try {
            rlock.lock();
            Thread.sleep(100);
            System.out.println("read finished");
            latch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rlock.unlock();
        }
    }

    void write(String str) {
        try {
            wlock.lock();
            Thread.sleep(1000);
            System.out.println("write finished");
            val = str;
            latch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            wlock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReadWriteLockTest t = new ReadWriteLockTest();

        long start = System.currentTimeMillis();
        for(int i=0;i<100;i++)
            new Thread(t::read,"读线程"+i).start();

        for(int i=0;i<20;i++)
            new Thread(()->{
                t.write(System.currentTimeMillis()+"写入");
            }).start();

        t.latch.await();
        long end = System.currentTimeMillis();
        System.out.println("耗时:"+(end-start));
    }
}

readwritelock

2. 如果使用普通锁整体效率低

public class ReadWriteLockTest2 {
    Lock lock = new ReentrantLock();

    String val = "读写锁";
    CountDownLatch latch = new CountDownLatch(120);

    void read() {
        try {
            lock.lock();
            Thread.sleep(10);
            System.out.println("read finished");
            latch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    void write(String str) {
        try {
            lock.lock();
            Thread.sleep(1000);
            System.out.println("write finished");
            val = str;
            latch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ReadWriteLockTest2 t = new ReadWriteLockTest2();

        long start = System.currentTimeMillis();
        for(int i=0;i<100;i++)
            new Thread(t::read,"读线程"+i).start();

        for(int i=0;i<20;i++)
            new Thread(()->{
                t.write(System.currentTimeMillis()+"写入");
            }).start();

        t.latch.await();
        long end = System.currentTimeMillis();
        System.out.println("耗时:"+(end-start));
    }
}

运行耗时:

readwritelock2

总结

ReadWriteLock 读写锁,在读时允许其他线程读,不允许写,在写时不允许其他线程读和写。
通过细分场景,在读多写少的场景中,提升了性能。

多线程系列在github上有一个开源项目,主要是本系列博客的实验代码。

https://github.com/forestnlp/concurrentlab

如果您对软件开发、机器学习、深度学习有兴趣请关注本博客,将持续推出Java、软件架构、深度学习相关专栏。

您的支持是对我最大的鼓励。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟空学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值