博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java读写锁实现数据同步访问
阅读量:7025 次
发布时间:2019-06-28

本文共 5736 字,大约阅读时间需要 19 分钟。

锁机制最大的改进之一就是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.000000Thread-4: Price1: 1.000000Thread-2: Price1: 1.000000Thread-2: Price2: 2.000000Thread-2: Price1: 1.000000Thread-2: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-0: Price1: 1.000000Thread-0: Price2: 2.000000Thread-2: Price1: 1.000000Thread-2: Price2: 2.000000Writer: Attempt to modify the prices.Writer: Prices have been modified.Thread-3: Price1: 1.000000Thread-3: Price2: 4.840562Thread-1: Price2: 2.000000Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Thread-1: Price1: 6.220535Thread-1: Price2: 4.840562Writer: Attempt to modify the prices.Writer: Prices have been modified.Thread-4: Price2: 2.000000Thread-4: Price1: 5.640719Thread-4: Price2: 1.872038Thread-4: Price1: 5.640719Thread-4: Price2: 1.872038Thread-2: Price1: 6.220535Thread-2: Price2: 1.872038Thread-2: Price1: 5.640719Thread-2: Price2: 1.872038Thread-2: Price1: 5.640719Thread-2: Price2: 1.872038Thread-2: Price1: 5.640719Thread-2: Price2: 1.872038Thread-2: Price1: 5.640719Thread-2: Price2: 1.872038Thread-2: Price1: 5.640719Thread-2: Price2: 1.872038Thread-2: Price1: 5.640719Thread-2: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Thread-3: Price1: 5.640719Thread-3: Price2: 1.872038Writer: Attempt to modify the prices.Writer: Prices have been modified.Thread-4: Price1: 5.491746Thread-4: Price2: 2.729420Thread-4: Price1: 5.491746Thread-4: Price2: 2.729420Thread-4: Price1: 5.491746Thread-4: Price2: 2.729420Thread-4: Price1: 5.491746Thread-4: Price2: 2.729420Thread-4: Price1: 5.491746Thread-4: Price2: 2.729420Thread-4: Price1: 5.491746Thread-4: Price2: 2.729420Thread-4: Price1: 5.491746Thread-4: Price2: 2.729420

转载地址:http://rwlxl.baihongyu.com/

你可能感兴趣的文章
jQuery方法position()与offset()区别
查看>>
Flume特点
查看>>
队列 句子分析 精辟的诠释 有图片
查看>>
在switch的default代码块中增加AssertionError错误
查看>>
JS:1.3,函数(function)
查看>>
Ubuntu下升级Git以及获取ssh keys的代码
查看>>
WebApi系列~开放的CORS,跨域资源访问对所有人开放
查看>>
在C#代码中应用Log4Net(一)简单使用Log4Net
查看>>
第 7 章 Gnu Gatekeeper
查看>>
java用while循环设计轮询线程的性能问题
查看>>
webservice 测试窗体只能用于来自本地计算机的请求
查看>>
14.9. 桌面支持
查看>>
部署Linux下的man慢查询中文帮助手册环境
查看>>
在VM重启之后,重新打开HANAStudio总是报用户名错误的解决方法
查看>>
算法分析实验题集
查看>>
模拟退火算法
查看>>
让WordPress主题支持语言本地化(使用poedit软件实现中文翻译功能)
查看>>
sql语句or与union all的执行效率比较
查看>>
[LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
查看>>
[WinAPI] API 6 [操作驱动器挂载点]
查看>>