锁机制最大的改进之一就是ReadWriteLock接口和它的唯一实现类ReentrantReadWriteLock。这个类有两个锁,一个是读操作锁,另一个是写操作锁。使用读操作锁时可以允许多个线程同时访问,但是使用写操作锁时只允许一个线程进行。在一个线程执行写操作时,其他线程不能够执行读操作。

  下面我们将通过范例学习如何使用ReadWriteLock接口编写程序。这个范例将使用ReadWriteLock接口控制对价格对象的访问,价格对象存储了两个产品的价格。

1. 创建一个价格信息类PricesInfo,并且存放两个产品的价格。

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; public class PricesInfo {
//两个价格
private double price1;
private double price2;
//声明读写锁ReadWriteLock对象lock
private ReadWriteLock lock;
public PricesInfo(){
price1 = 1.0;
price2 = 2.0;
lock = new ReentrantReadWriteLock();
} public double getPrice1(){
lock.readLock().lock();
double value = price1;
lock.readLock().unlock();
return value;
} public double getPrice2()
{
lock.readLock().lock();
double value = price2;
lock.readLock().unlock();
return value;
} public void setPrices(double price1, double price2){
lock.writeLock().lock();
this.price1 = price1;
this.price2 = price2;
lock.writeLock().unlock();
} }

2. 创建读取类Reader,它实现了Runnable接口。

public class Reader implements Runnable {
private PricesInfo pricesInfo;
public Reader(PricesInfo pricesInfo){
this.pricesInfo = pricesInfo;
}
@Override
public void run() {
// 循环读取连个价格10次
for(int i=0;i<10;i++){
System.out.printf("%s: Price1: %f\n", Thread.currentThread().getName(), pricesInfo.getPrice1());
System.out.printf("%s: Price2: %f\n", Thread.currentThread().getName(), pricesInfo.getPrice2());
} } }

3. 创建写入类Writer,它实现了Runnable接口。

public class Writer implements Runnable {
private PricesInfo pricesInfo;
public Writer(PricesInfo pricesInfo){
this.pricesInfo = pricesInfo;
}
@Override
public void run() {
// 循环修改两个价格3次
try {
for(int i=0;i<3;i++){
System.out.printf("Writer: Attempt to modify the prices.\n");
pricesInfo.setPrices(Math.random()*10, Math.random()*8);
System.out.println("Writer: Prices have been modified.");
Thread.sleep(2);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

4. 创建范例的主类Main

public class Main {

    public static void main(String[] args) {
PricesInfo pricesInfo = new PricesInfo();
Reader[] readers = new Reader[5];
Thread[] threadsReader = new Thread[5];
for(int i=0;i<5;i++){
readers[i] = new Reader(pricesInfo);
threadsReader[i] = new Thread(readers[i]);
}
Writer writer = new Writer(pricesInfo);
Thread threadWriter = new Thread(writer);
for(int i=0;i<5;i++){
threadsReader[i].start();
}
threadWriter.start();
}
}

5. 程序运行结果如下

Thread-1: Price1: 1.000000
Thread-4: Price1: 1.000000
Thread-2: Price1: 1.000000
Thread-2: Price2: 2.000000
Thread-2: Price1: 1.000000
Thread-2: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-0: Price1: 1.000000
Thread-0: Price2: 2.000000
Thread-2: Price1: 1.000000
Thread-2: Price2: 2.000000
Writer: Attempt to modify the prices.
Writer: Prices have been modified.
Thread-3: Price1: 1.000000
Thread-3: Price2: 4.840562
Thread-1: Price2: 2.000000
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Thread-1: Price1: 6.220535
Thread-1: Price2: 4.840562
Writer: Attempt to modify the prices.
Writer: Prices have been modified.
Thread-4: Price2: 2.000000
Thread-4: Price1: 5.640719
Thread-4: Price2: 1.872038
Thread-4: Price1: 5.640719
Thread-4: Price2: 1.872038
Thread-2: Price1: 6.220535
Thread-2: Price2: 1.872038
Thread-2: Price1: 5.640719
Thread-2: Price2: 1.872038
Thread-2: Price1: 5.640719
Thread-2: Price2: 1.872038
Thread-2: Price1: 5.640719
Thread-2: Price2: 1.872038
Thread-2: Price1: 5.640719
Thread-2: Price2: 1.872038
Thread-2: Price1: 5.640719
Thread-2: Price2: 1.872038
Thread-2: Price1: 5.640719
Thread-2: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Thread-3: Price1: 5.640719
Thread-3: Price2: 1.872038
Writer: Attempt to modify the prices.
Writer: Prices have been modified.
Thread-4: Price1: 5.491746
Thread-4: Price2: 2.729420
Thread-4: Price1: 5.491746
Thread-4: Price2: 2.729420
Thread-4: Price1: 5.491746
Thread-4: Price2: 2.729420
Thread-4: Price1: 5.491746
Thread-4: Price2: 2.729420
Thread-4: Price1: 5.491746
Thread-4: Price2: 2.729420
Thread-4: Price1: 5.491746
Thread-4: Price2: 2.729420
Thread-4: Price1: 5.491746
Thread-4: Price2: 2.729420

java读写锁实现数据同步访问的更多相关文章

  1. 线程同步——用户模式下线程同步——Slim读写锁实现线程同步

    //Slim读/写锁实现线程同步 SRWlock 的目的和关键段相同:对同一资源进行保护,不让其它线程访问. 但是,与关键段不同的是,SRWlock允许我们区分哪些想要读取资源的线程(读取者线程) 和 ...

  2. Java读写锁

    Java读写锁,ReadWriteLock.java接口, RentrantReadWriteLock.java实现.通过读写锁,可以实现读-读线程并发,读-写,写-读线程互斥进行.以前面试遇到一个问 ...

  3. Java并发指南10:Java 读写锁 ReentrantReadWriteLock 源码分析

    Java 读写锁 ReentrantReadWriteLock 源码分析 转自:https://www.javadoop.com/post/reentrant-read-write-lock#toc5 ...

  4. java 读写锁详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt124 在java多线程中,为了提高效率有些共享资源允许同时进行多个读的操作, ...

  5. Java 读写锁的实现

    一.    synchronized和ReentrantLock的对比 到现在,看到多线程中,锁定的方式有2种:synchronized和ReentrantLock.两种锁定方式各有优劣,下面简单对比 ...

  6. Java读写锁(ReentrantReadWriteLock)学习

    什么是读写锁 平时,我们常见的synchronized和Reentrantlock基本上都是排他锁,这些锁在同一时刻只允许一个线程进行访问,哪怕是读操作.而读写锁是维护了一对锁(一个读锁和一个写锁), ...

  7. 从火车站车次公示栏来学Java读写锁

    Java多线程并发之读写锁 本文主要内容:读写锁的理论:通过生活中例子来理解读写锁:读写锁的代码演示:读写锁总结.通过理论(总结)-例子-代码-然后再次总结,这四个步骤来让大家对读写锁的深刻理解. 本 ...

  8. Java 读写锁 ReadWriteLock 原理与应用场景详解

    Java并发编程提供了读写锁,主要用于读多写少的场景,今天我就重点来讲解读写锁的底层实现原理@mikechen 什么是读写锁? 读写锁并不是JAVA所特有的读写锁(Readers-Writer Loc ...

  9. Java多线程学习笔记——从Java JVM对多线程数据同步的一些理解

       我们知道在多线程编程中,我们很大的一部分内容是为了解决线程间的资源同步问题和线程间共同协作解决问题.线程间的同步,通俗我们理解为僧多粥少,在粥有限情况下,我们怎么去防止大家有秩序的喝到粥,不至于 ...

随机推荐

  1. NLP基础 成分句法分析和依存句法分析

    正则匹配: .除换行符所有的 ?表示0次或者1次 *表示0次或者n次 a(bc)+表示bc至少出现1次 ^x.*g$表示字符串以x开头,g结束 |或者 http://regexr.com/ 依存句法分 ...

  2. 认识hasLayout——IE浏览器css bug的一大罪恶根源 (转)

    认识hasLayout--IE浏览器css bug的一大罪恶根源 转 什么是hasLayout?hasLayout是IE特有的一个属性.很多的ie下的css bug都与其息息相关.在ie中,一个元素要 ...

  3. vscode vue配置和一些其它辅助【工具篇】

    后续有补充就经常更新

  4. 移动端禁止图片长按和vivo手机点击img标签放大图片,禁止长按识别二维码或保存图片【转载】

    移动端禁止图片长按和vivo手机点击img标签放大图片,禁止长按识别二维码或保存图片 img{ pointer-events: none; } 源文地址:https://www.cnblogs.com ...

  5. 【C#】Unicode的流言终结者和编码大揭秘

    如果你是一个生活在2003年的程序员,却不了解字符.字符集.编码和Unicode这些基础知识.那你可要小心了,要是被我抓到你,我会让你在潜水艇里剥六个月洋葱来惩罚你. 这个邪恶的恐吓是Joel Spo ...

  6. 【WPF】设置Listview样式

    <ListView Grid.Row="0" ItemsSource="{Binding Lst_bind}"> <ListView.Item ...

  7. Python并发编程-进程池回调函数

    回调函数不能传参数 回调函数是在主进程中执行的 from multiprocessing import Pool import os def func1(n): print('in func1', o ...

  8. mybatis递归,一对多代码示例

    今天需要做一个功能,根据专业,有不同的章节,章节下面有对应的习题, 由于只有这么两级,可以不用使用递归,直接查询父集,之后foreach查询子集放入对应的list集合. 虽然实现了,感觉毕竟,太low ...

  9. Python 脚本编程及国际化

    在前一篇博客文章 <使用 Python 编写脚本并发布> 中,我介绍了如何使用 Python 进行脚本编程,说实话这是我在尝试 Python 进行网站和网络编程之后首次使用 Python ...

  10. Bzoj2510 弱题(矩阵快速幂)

    题面(权限题) 题解 一道概率\(dp\),可以设\(f[i][j]\)表示第\(i\)次操作后,标号为\(j\)的小球的期望个数,那么有: \[ \begin{aligned} &f[i][ ...