当前位置: 首页 > news >正文

【juc】读写锁ReentrantReadWriteLock

目录

        • 一、说明
        • 二、读读不互斥
          • 2.1 代码示例
          • 2.2 截图示例
        • 三、读写互斥
          • 3.1 代码示例
          • 3.2 截图示例
        • 四、写写互斥
          • 4.1 代码示例
          • 4.2 截图示例
        • 五、注意事项
          • 5.2.1 代码示例
          • 5.2.2 截图示例

一、说明

  • 1.当读操作远远高于写操作时,使用读写锁让读读可以并发,来提高性能
  • 2.类似于数据库中的select … from … lock in share mode
  • 3.提供一个数据容器类,内部分别使用读锁保护数据的read()方法,写锁保护数据的write()方法

二、读读不互斥

2.1 代码示例
package com.learning;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantReadWriteLock;@Slf4j
public class ReadWriteLockLearning {private Object data;private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();private ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();private ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();public Object read(){log.debug("获取读锁");readLock.lock();try {log.debug("读取");try {Thread.sleep(1000);}catch(Exception e){e.printStackTrace();}return data;}finally {log.debug("释放读锁");readLock.unlock();}}public void write(){log.debug("获取写锁");writeLock.lock();try{log.debug("写入");}finally {log.debug("释放写锁");writeLock.unlock();}}public static void main(String[] args) {ReadWriteLockLearning readWriteLockLearning = new ReadWriteLockLearning();new Thread(()->{readWriteLockLearning.read();}, "t1").start();new Thread(()->{readWriteLockLearning.read();}, "t2").start();}
}
2.2 截图示例

在这里插入图片描述

三、读写互斥

3.1 代码示例
package com.learning;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantReadWriteLock;@Slf4j
public class ReadWriteLockLearning {private Object data;private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();private ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();private ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();public Object read(){log.debug("获取读锁");readLock.lock();try {log.debug("读取");return data;}finally {log.debug("释放读锁");readLock.unlock();}}public void write(){log.debug("获取写锁");writeLock.lock();try{log.debug("写入");try {Thread.sleep(1000);}catch(Exception e){e.printStackTrace();}}finally {log.debug("释放写锁");writeLock.unlock();}}public static void main(String[] args) {ReadWriteLockLearning readWriteLockLearning = new ReadWriteLockLearning();new Thread(()->{readWriteLockLearning.read();}, "t1").start();try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}new Thread(()->{readWriteLockLearning.write();}, "t2").start();}
}
3.2 截图示例

在这里插入图片描述

四、写写互斥

4.1 代码示例
package com.learning;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantReadWriteLock;@Slf4j
public class ReadWriteLockLearning {private Object data;private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();private ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();private ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();public Object read(){log.debug("获取读锁");readLock.lock();try {log.debug("读取");return data;}finally {log.debug("释放读锁");readLock.unlock();}}public void write(){log.debug("获取写锁");writeLock.lock();try{log.debug("写入");try {Thread.sleep(1000);}catch(Exception e){e.printStackTrace();}}finally {log.debug("释放写锁");writeLock.unlock();}}public static void main(String[] args) {ReadWriteLockLearning readWriteLockLearning = new ReadWriteLockLearning();new Thread(()->{readWriteLockLearning.write();}, "t1").start();try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}new Thread(()->{readWriteLockLearning.write();}, "t2").start();}
}
4.2 截图示例

在这里插入图片描述

五、注意事项

  • 1.读锁不支持条件变量
  • 2.不支持重入时升级:持有读锁的情况下去获取写锁,会导致获取写锁永久等待
5.2.1 代码示例
package com.learning;import lombok.extern.slf4j.Slf4j;import java.util.concurrent.locks.ReentrantReadWriteLock;@Slf4j
public class ReadWriteLockLearning2 {private Object data;private ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();private ReentrantReadWriteLock.ReadLock readLock = reentrantReadWriteLock.readLock();private ReentrantReadWriteLock.WriteLock writeLock = reentrantReadWriteLock.writeLock();public void readWrite(){log.debug("获取读锁");readLock.lock();try {try{log.debug("获取写锁");writeLock.lock();}finally {log.debug("释放写锁");writeLock.unlock();}}finally {log.debug("释放读锁");readLock.unlock();}}public static void main(String[] args) {ReadWriteLockLearning2 readWriteLockLearning = new ReadWriteLockLearning2();readWriteLockLearning.readWrite();}
}
5.2.2 截图示例

在这里插入图片描述

  • 3.支持重入时降级:持有写锁的情况下去获取读锁
http://www.lryc.cn/news/151790.html

相关文章:

  • Linux开机启动Tomcat
  • javaweb、spring、springmvc和springboot有什么区别,都是做什么用的?
  • 已解决module ‘pip‘ has no attribute ‘pep425tags‘报错问题(如何正确查看pip版本、支持、32位、64位方法汇总)
  • Matlab(画图初阶)
  • 汽车自适应巡航系统控制策略研究
  • C语言面试题值反转字符串
  • 【大数据】Apache Iceberg 概述和源代码的构建
  • 对分库分表进行批量操作
  • 大数据组件-Flume集群环境的启动与验证
  • 【包过滤防火墙——iptables静态防火墙】的简单使用
  • 关于MySQL数据库版本不同导致表进行比较的时候报错illegal mix of collations...的问题
  • 进程、操作系统
  • hadoop学习:mapreduce入门案例四:partitioner 和 combiner
  • HTTP与SOCKS5的区别对比
  • 在阿里云请求发短信接口去掉证书验证
  • k8s里pv pvc configmap
  • 【Atcoder】 [ARC144D] AND OR Equation
  • python使用字典暴力解析wifi密码
  • java八股文面试[多线程]——synchronized锁升级详细流程
  • ui网页设计实训心得
  • 论文阅读_扩散模型_DDPM
  • 菜鸟教程《Python 3 教程》笔记(15):数据结构
  • CH05_介绍重构名录
  • 1、Nginx 简介
  • C++之——宏
  • 代码随想录打卡—day56—【编辑距离】— 9.2 编辑距离系列
  • uni-app app端.m3u8类型流的播放
  • 使用proxy_pool来为爬虫程序自动更换代理IP | 开源IP代理
  • 【易售小程序项目】修改“我的”界面前端实现;查看、重新编辑、下架自己发布的商品【后端基于若依管理系统开发】
  • Centos7 + Apache Ranger 2.4.0 部署