JDK多线程基础(9):读写分离锁 ReadWriteLock

读写锁 ReadWriteLock

简介

  1. 读写分离锁是减少锁粒度的一种特殊情况,是对系统功能点的分割,其分割了读与写两种操作
  2. 适合场景:读多写少的场合。交互逻辑如下:
  • 读锁之间是相容的,读锁在读读操作是不需要相互等待,提升多线程读数据的性能
  • 写锁的占用是独占的
读锁写锁
读锁可访问不可访问
写锁不可访问不可访问

简单实现

public class ReadWriteLockTest {
    public static void main(String[] args) {
        Queue tQueue = new Queue();
        new Thread(new ReadThread(tQueue), "read_1").start();
        new Thread(new ReadThread(tQueue), "read_2").start();

        new Thread(new WriteThread(tQueue), "write_1").start();
        new Thread(new WriteThread(tQueue), "write_2").start();
    }


    /**
     * 读的线程
     */
    static class ReadThread implements Runnable {
        private Queue tQueue;
        public ReadThread(Queue tQueue) {
            this.tQueue = tQueue;
        }

        @Override
        public void run() {
            for (int i = 0; i < 5; i++) {
                tQueue.read();
            }
        }
    }

    /**
     * 写的线程
     */
    static class WriteThread implements Runnable {
        private Queue tQueue;
        public WriteThread(Queue tQueue) {
            this.tQueue = tQueue;
        }

        @Override
        public void run() {
            for (int i = 3; i < 7; i++) {
                tQueue.write("data_ " + i);
            }
        }
    }

    /**
     * @author SAM-SHO
     * @title 具体业务类
     * @description
     * @Date 2014-8-23
     */
    static class Queue {
        /**
         * 共享数据,只能有一个线程能写该数据,但可以多个线程同时读数据
         */
        private Object data = null;

        /**
         * 使用读写锁
         */
        ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

        /**
         * 读数据的方法
         */
        public void read() {

            readWriteLock.readLock().lock();//上锁
            try {

                System.out.println("开始读数据....." + Thread.currentThread().getName());
                Thread.sleep((long) (Math.random() * 1000));
                System.out.println(Thread.currentThread().getName() + "已经读到数据: " + data);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                readWriteLock.readLock().unlock();//解锁
            }

        }

        /**
         * 写数据的方法
         *
         * @param tData
         */
        public void write(Object tData) {

            readWriteLock.writeLock().lock();//上锁

            try {
                System.out.println("准备写数据....." + Thread.currentThread().getName());
                Thread.sleep((long) (Math.random() * 1000));
                this.data = tData;
                System.out.println(Thread.currentThread().getName() + "已经写完数据: " + data);

            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                readWriteLock.writeLock().unlock();//解锁
            }

        }

    }

}

参考

  1. 源码地址

Fork me on Gitee

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值