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

ReentranReadWriteLock 使用案例

 ReentranReadWriteLock使用案例

/*** ReentranReadWriteLock 使用案例* 读线程共享* 写线程互斥*/
public class ReentrantReadWriteLockExample {private String news;private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();public String readNews() {lock.readLock().lock();try {// 读线程睡眠1秒, 观察打印时间Thread.sleep(1000);System.out.println(Thread.currentThread().getName() + " " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss")) + " is reading the news: " + news);return news;} catch (InterruptedException e) {throw new RuntimeException(e);} finally {lock.readLock().unlock();}}public void writeNews(String updatedNews) {lock.writeLock().lock();try {// 让写线程睡眠两秒,这样可以观察到写线程阻塞Thread.sleep(2000); System.out.println(Thread.currentThread().getName() + " " +  LocalDateTime.now().format(DateTimeFormatter.ofPattern("hh:mm:ss"))  + " is updating the news to: " + updatedNews);news = updatedNews;} catch (InterruptedException e) {throw new RuntimeException(e);} finally {lock.writeLock().unlock();}}public static void main(String[] args) {ReentrantReadWriteLockExample newsData = new ReentrantReadWriteLockExample();// 创建多个读线程for (int i = 0; i < 10; i++) {new Thread(() -> {newsData.readNews();}).start();}// 创建多个写线程for (int i = 0; i < 5; i++) {new Thread(()->{newsData.writeNews("Breaking news: JavaAssistant is awesome!");}).start();}}
}

 输出结果

从结果可以看出,读线程是同时读取了news,而写线程则是一个个的读。

Thread-0 10:11:45 is reading the news: null
Thread-7 10:11:45 is reading the news: null
Thread-6 10:11:45 is reading the news: null
Thread-1 10:11:45 is reading the news: null
Thread-5 10:11:45 is reading the news: null
Thread-3 10:11:45 is reading the news: null
Thread-2 10:11:45 is reading the news: null
Thread-9 10:11:45 is reading the news: null
Thread-4 10:11:45 is reading the news: null
Thread-8 10:11:45 is reading the news: null
Thread-10 10:11:47 is updating the news to: Breaking news: JavaAssistant is awesome!
Thread-11 10:11:49 is updating the news to: Breaking news: JavaAssistant is awesome!
Thread-12 10:11:51 is updating the news to: Breaking news: JavaAssistant is awesome!
Thread-13 10:11:53 is updating the news to: Breaking news: JavaAssistant is awesome!
Thread-14 10:11:55 is updating the news to: Breaking news: JavaAssistant is awesome!

http://www.lryc.cn/news/236481.html

相关文章:

  • “我们把最扎心的话,说给了自己最亲近的人” 何解?| IDCF
  • MongoDB之索引和聚合
  • 【GEE】基于GEE进行非监督学习
  • 多视图聚类的论文阅读(一)
  • K-Means算法进行分类
  • 深度学习交通车辆流量分析 - 目标检测与跟踪 - python opencv 计算机竞赛
  • 网络协议入门 笔记一
  • 系列十一、你平时工作用过的JVM常用基本配置参数有哪些?
  • 如何为视频添加旁白,有哪些操作技巧?
  • 如何简单挖掘公益SRC?
  • PhpStorm激活
  • mysql 怎么做定时备份 / mysql 备份 / sql文件导出
  • 416. 分割等和子集问题(动态规划)
  • 【软件安装】Centos系统中安装docker容器(华为云HECS云耀服务器)
  • GitHub Proxy 快速下载github文件
  • 大厂秋招真题【栈】Bilibili2019秋招-简单表达式求值
  • (一)RISC-V 指令集及寄存器介绍
  • 二十三种设计模式:解密职责链模式-购物优惠活动的设计艺术
  • 竞赛 题目:基于深度学习卷积神经网络的花卉识别 - 深度学习 机器视觉
  • unexpected end of stream on
  • 【微信小程序篇】- 组件
  • 使用Sqoop命令从Oracle同步数据到Hive,修复数据乱码 %0A的问题
  • NC Cloud uploadChunk文件上传漏洞复现
  • 多标签页之间的通信
  • CI/CD -gitlab
  • AR眼镜_单目光波导VS双目光波导方案
  • golang 动态库 (buildmode)
  • 【mysql】2006 - Server has gone away
  • 动态规划43(Leetcode91解码方法)
  • STM32电源名词解析