java并发之读写锁ReentrantReadWriteLock的使用
Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象。两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象。
读写锁:分为读锁和写锁,多个读锁不互斥,读锁与写锁互斥,这是由jvm自己控制的,你只要上好相应的锁即可。如果你的代码只读数据,可以很多人同时读,但不能同时写,那就上读锁;如果你的代码修改数据,只能有一个人在写,且不能同时读取,那就上写锁。总之,读的时候上读锁,写的时候上写锁!
ReentrantReadWriteLock会使用两把锁来解决问题,一个读锁,一个写锁
线程进入读锁的前提条件:
没有其他线程的写锁,
没有写请求或者有写请求,但调用线程和持有锁的线程是同一个
线程进入写锁的前提条件:
没有其他线程的读锁
没有其他线程的写锁
到ReentrantReadWriteLock,首先要做的是与ReentrantLock划清界限。它和后者都是单独的实现,彼此之间没有继承或实现的关系。然后就是总结这个锁机制的特性了:
(a).重入方面其内部的WriteLock可以获取ReadLock,但是反过来ReadLock想要获得WriteLock则永远都不要想。
(b).WriteLock可以降级为ReadLock,顺序是:先获得WriteLock再获得ReadLock,然后释放WriteLock,这时候线程将保持Readlock的持有。反过来ReadLock想要升级为WriteLock则不可能,为什么?参看(a),呵呵.
(c).ReadLock可以被多个线程持有并且在作用时排斥任何的WriteLock,而WriteLock则是完全的互斥。这一特性最为重要,因为对于高读取频率而相对较低写入的数据结构,使用此类锁同步机制则可以提高并发量。
(d).不管是ReadLock还是WriteLock都支持Interrupt,语义与ReentrantLock一致。
(e).WriteLock支持Condition并且与ReentrantLock语义一致,而ReadLock则不能使用Condition,否则抛出UnsupportedOperationException异常。
下面看一个读写锁的例子:
1 /** 2 * 模拟数据库表 读数据 写数据 3 * @author ko 4 * 5 */ 6 public class DataQueue implements Runnable { 7 8 private int randomNum;// 随机数 9 private List<String> dataList;// 存放数据的集合 10 private ReentrantReadWriteLock rwLock;// 读写锁 11 12 public DataQueue(int randomNum, List<String> dataList, ReentrantReadWriteLock rwLock) { 13 super(); 14 this.randomNum = randomNum; 15 this.dataList = dataList; 16 this.rwLock = rwLock; 17 } 18 19 public void getData(){ 20 rwLock.readLock().lock();// 开启读锁 只能允许读的线程访问 21 System.out.println("read thread "+Thread.currentThread().getName()+" begin read data"); 22 StringBuffer sb = new StringBuffer(); 23 for (String data : dataList) { 24 sb.append(data+" "); 25 } 26 System.out.println("read thread "+Thread.currentThread().getName()+" read data:"+sb.toString()); 27 System.out.println("read thread "+Thread.currentThread().getName()+" end read data"); 28 rwLock.readLock().unlock();// 释放读锁 29 } 30 31 public void setData(){ 32 rwLock.writeLock().lock();// 开启写锁 其它线程不管是读还是写都不能访问 33 System.out.println("write thread "+Thread.currentThread().getName()+" begin write data"); 34 String data = UUID.randomUUID().toString(); 35 dataList.add(data); 36 System.out.println("write thread "+Thread.currentThread().getName()+" write data:"+data); 37 System.out.println("write thread "+Thread.currentThread().getName()+" end write data"); 38 rwLock.writeLock().unlock();// 释放读锁 39 } 40 41 @Override 42 public void run() { 43 if (randomNum%2 == 0) { 44 getData(); 45 } else { 46 setData(); 47 } 48 } 49 }
1 /** 2 * 利用ReentrantReadWriteLock模拟数据的读写分离 3 * @author ko 4 * 5 */ 6 public class DatabaseReadWriteSeparation { 7 8 public static void main(String[] args) { 9 List<String> dataList = new ArrayList<>(); 10 ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(); 11 // DataQueue dataQueue = ; 12 ExecutorService exec = Executors.newCachedThreadPool(); 13 for (int i = 0; i < 10; i++) { 14 exec.execute(new DataQueue(new Random().nextInt(10), dataList, rwLock)); 15 } 16 exec.shutdown(); 17 } 18 }
read thread pool-1-thread-3 begin read data read thread pool-1-thread-2 begin read data read thread pool-1-thread-3 read data: read thread pool-1-thread-2 read data: read thread pool-1-thread-3 end read data read thread pool-1-thread-2 end read data write thread pool-1-thread-1 begin write data write thread pool-1-thread-1 write data:73a8bfcc-7cb9-4a36-aa06-ecbf90c3e612 write thread pool-1-thread-1 end write data write thread pool-1-thread-5 begin write data write thread pool-1-thread-5 write data:c334bb7a-1dfe-4f64-a996-1ba6f714710e write thread pool-1-thread-5 end write data read thread pool-1-thread-4 begin read data read thread pool-1-thread-6 begin read data read thread pool-1-thread-4 read data:73a8bfcc-7cb9-4a36-aa06-ecbf90c3e612 c334bb7a-1dfe-4f64-a996-1ba6f714710e read thread pool-1-thread-4 end read data read thread pool-1-thread-6 read data:73a8bfcc-7cb9-4a36-aa06-ecbf90c3e612 c334bb7a-1dfe-4f64-a996-1ba6f714710e read thread pool-1-thread-6 end read data write thread pool-1-thread-7 begin write data write thread pool-1-thread-7 write data:7266821f-dc72-4a17-8891-6b7ec80a047b write thread pool-1-thread-7 end write data write thread pool-1-thread-8 begin write data write thread pool-1-thread-8 write data:e5fd7de9-3b5c-4a50-8dcb-539d3ca398fd write thread pool-1-thread-8 end write data read thread pool-1-thread-10 begin read data read thread pool-1-thread-10 read data:73a8bfcc-7cb9-4a36-aa06-ecbf90c3e612 c334bb7a-1dfe-4f64-a996-1ba6f714710e 7266821f-dc72-4a17-8891-6b7ec80a047b e5fd7de9-3b5c-4a50-8dcb-539d3ca398fd read thread pool-1-thread-10 end read data read thread pool-1-thread-9 begin read data read thread pool-1-thread-9 read data:73a8bfcc-7cb9-4a36-aa06-ecbf90c3e612 c334bb7a-1dfe-4f64-a996-1ba6f714710e 7266821f-dc72-4a17-8891-6b7ec80a047b e5fd7de9-3b5c-4a50-8dcb-539d3ca398fd read thread pool-1-thread-9 end read data
从打印的结果可以看出当读的时候线程2 3、4 6、9 10分别是同时两两进行的,写的时候线程5、7、8分别是单独进行的。