Java并发编程-09-使用读写锁同步数据访问

一、ReadWriteLock接口


1、可以看出,这个是java1.5添加的

2、主要的实现类是ReentrantReadWriteLock

3、主要的方法是readLock()和writeLock()

二、测试代码

package com.concurrent.threadSynchronize;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class PricesInfo {

	private double prices1;
	private double prices2;

	// 定义读写锁
	private ReadWriteLock readWriteLock;

	public PricesInfo() {
		this.prices1 = 1.0;
		this.prices2 = 2.0;
		readWriteLock = new ReentrantReadWriteLock();
	}

	public double getPrices1() {
		readWriteLock.readLock().lock();
		double value = prices1;
		readWriteLock.readLock().unlock();
		return value;
	}

	public double getPrices2() {
		readWriteLock.readLock().lock();
		double value = prices2;
		readWriteLock.readLock().unlock();
		return value;
	}

	public void setPrices(double prices1, double prices2) {

		readWriteLock.writeLock().lock();
		this.prices1 = prices1;
		this.prices2 = prices2;
		readWriteLock.writeLock().unlock();
	}
}

package com.concurrent.threadSynchronize;

public class Reader implements Runnable {

	private PricesInfo pricesInfo;

	public Reader(PricesInfo pricesInfo) {
		this.pricesInfo = pricesInfo;
	}

	//循环读取价格10次
	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			System.out.printf("%s : Price 1 : %f\n", Thread.currentThread()
					.getName(), pricesInfo.getPrices1());
			System.out.printf("%s : Price 2 : %f\n", Thread.currentThread()
					.getName(), pricesInfo.getPrices2());
		}
	}
}

package com.concurrent.threadSynchronize;

public class Writer implements Runnable {

	private PricesInfo pricesInfo;

	public Writer(PricesInfo pricesInfo) {
		this.pricesInfo = pricesInfo;
	}

	// 循环修改价格三次
	@Override
	public void run() {
		for (int i = 0; i < 3; i++) {
			System.out.println("Writer : Attempt to modify the prices.");
			pricesInfo.setPrices(Math.random() * 10, Math.random() * 8);
			System.out.println("Writer : The prices has been modified.");

			try {
				Thread.sleep(2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

package com.concurrent.threadSynchronize;

public class Main2 {

	public static void main(String[] args) {
		PricesInfo pricesInfo = new PricesInfo();

		Reader[] readers = new Reader[5];

		Thread[] threadReaders = new Thread[5];

		for (int i = 0; i < 5; i++) {
			readers[i] = new Reader(pricesInfo);
			threadReaders[i] = new Thread(readers[i]);
		}

		Writer writer = new Writer(pricesInfo);
		Thread threadWriter = new Thread(writer);

		// 启动这六个线程
		for (int i = 0; i < 5; i++) {
			threadReaders[i].start();
		}
		threadWriter.start();
	}

}

结果:

Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-2 : Price 1 : 1.000000
Thread-2 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Thread-3 : Price 1 : 1.000000
Thread-3 : Price 2 : 2.000000
Writer : Attempt to modify the prices.
Thread-4 : Price 1 : 1.000000
Thread-4 : Price 2 : 2.000000
Thread-4 : Price 1 : 3.783085
Thread-4 : Price 2 : 6.706074
Thread-4 : Price 1 : 3.783085
Thread-4 : Price 2 : 6.706074
Thread-4 : Price 1 : 3.783085
Thread-0 : Price 1 : 1.000000
Thread-0 : Price 2 : 6.706074
Thread-0 : Price 1 : 3.783085
Thread-0 : Price 2 : 6.706074
Thread-0 : Price 1 : 3.783085
Thread-0 : Price 2 : 6.706074
Thread-0 : Price 1 : 3.783085
Thread-0 : Price 2 : 6.706074
Thread-0 : Price 1 : 3.783085
Thread-0 : Price 2 : 6.706074
Thread-1 : Price 1 : 1.000000
Thread-0 : Price 1 : 3.783085
Thread-4 : Price 2 : 6.706074
Thread-4 : Price 1 : 3.783085
Writer : The prices has been modified.
Thread-4 : Price 2 : 6.706074
Thread-4 : Price 1 : 3.783085
Thread-4 : Price 2 : 6.706074
Thread-4 : Price 1 : 3.783085
Thread-4 : Price 2 : 6.706074
Thread-4 : Price 1 : 3.783085
Thread-4 : Price 2 : 6.706074
Thread-4 : Price 1 : 3.783085
Thread-4 : Price 2 : 6.706074
Thread-4 : Price 1 : 3.783085
Thread-4 : Price 2 : 6.706074
Thread-0 : Price 2 : 6.706074
Thread-0 : Price 1 : 3.783085
Thread-0 : Price 2 : 6.706074
Thread-0 : Price 1 : 3.783085
Thread-0 : Price 2 : 6.706074
Thread-0 : Price 1 : 3.783085
Thread-0 : Price 2 : 6.706074
Thread-0 : Price 1 : 3.783085
Thread-0 : Price 2 : 6.706074
Thread-1 : Price 2 : 6.706074
Thread-1 : Price 1 : 3.783085
Thread-1 : Price 2 : 6.706074
Thread-1 : Price 1 : 3.783085
Thread-1 : Price 2 : 6.706074
Thread-1 : Price 1 : 3.783085
Thread-1 : Price 2 : 6.706074
Thread-1 : Price 1 : 3.783085
Thread-1 : Price 2 : 6.706074
Thread-1 : Price 1 : 3.783085
Thread-1 : Price 2 : 6.706074
Thread-1 : Price 1 : 3.783085
Writer : Attempt to modify the prices.
Thread-1 : Price 2 : 6.706074
Writer : The prices has been modified.
Thread-1 : Price 1 : 9.636421
Thread-1 : Price 2 : 0.480659
Thread-1 : Price 1 : 9.636421
Thread-1 : Price 2 : 0.480659
Thread-1 : Price 1 : 9.636421
Thread-1 : Price 2 : 0.480659
Writer : Attempt to modify the prices.
Writer : The prices has been modified.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值