一、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.