读写锁,分为读锁和写锁,多个读锁不互斥,读锁和写锁互斥,写锁与写锁互斥,这是JVM自己控制的,你只要上好相应的锁即可,如果你的代码只读数据,可以很多人同时读,但不能同时写,那就上读锁;如果你的代码修改数据,只能有一个人在写,且不能同时读取,那就上写锁.总之,读的时候上读锁,写的时候上写锁!
看如下程序: 新建6个线程,3个线程用来读,3个线程用来写,
package javaplay.thread.test;import java.util.Random;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class ReadWriteLockTest { public static void main(String[] args) { final Queue3 q3 = new Queue3(); for (int i = 0; i < 3; i++) { new Thread() { public void run() { while (true) { q3.get(); } } }.start(); new Thread() { public void run() { while (true) { q3.put(new Random().nextInt(10000)); } } }.start(); } }}class Queue3 { private Object data = null;// 共享数据,只能有一个线程能写该数据,但可以有多个线程同时读该数据。 // 读写锁 ReadWriteLock rwl = new ReentrantReadWriteLock(); // 相当于读操作 public void get() { rwl.readLock().lock(); try { System.out.println(Thread.currentThread().getName() + " be ready to read data!"); Thread.sleep((long) (Math.random() * 1000)); System.out.println(Thread.currentThread().getName() + "have read data :" + data); } catch (InterruptedException e) { e.printStackTrace(); } finally { rwl.readLock().unlock(); } } // 相当于写操作 public void put(Object data) { rwl.writeLock().lock(); try { System.out.println(Thread.currentThread().getName() + " be ready to write data!"); Thread.sleep((long) (Math.random() * 1000)); this.data = data; System.out.println(Thread.currentThread().getName() + " have write data: " + data); } catch (InterruptedException e) { e.printStackTrace(); } finally { rwl.writeLock().unlock(); } }}
读写锁功能很强大!这样可以实现正常的逻辑,如果我们把读写锁相关的代码注释,发现程序正准备写的时候,就有线程读了,发现准备读的时候,有线程去写,这样不符合我们的逻辑;通过Java5的新特新可以很轻松的解决这样的问题;
查看Java API ReentrantReadWriteLock 上面有经典(缓存)的用法,下面是doc里面的伪代码,,它演示的是一个实体的缓存,不是缓存系统,相当于缓存代理,注意volatile的运用:
package javaplay.thread.test;import java.util.concurrent.locks.ReentrantReadWriteLock;/* * Sample usages. Here is a code sketch showing how to perform lock downgrading after updating a cache * (exception handling is particularly tricky when handling multiple locks in a non-nested fashion): */class CachedData { Object data; volatile boolean cacheValid; final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); void processCachedData() { rwl.readLock().lock(); if (!cacheValid) { // Must release read lock before acquiring write lock rwl.readLock().unlock(); rwl.writeLock().lock(); try { // Recheck state because another thread might have // acquired write lock and changed state before we did. if (!cacheValid) { data = ... cacheValid = true; } // Downgrade by acquiring read lock before releasing write lock rwl.readLock().lock(); } finally { rwl.writeLock().unlock(); // Unlock write, still hold read } } try { use(data); } finally { rwl.readLock().unlock(); } }}
假设现在多个线程来读了,那第一个线程读到的数据是空的,那它就要写就要填充数据,那么第二个第三个就应该互斥等着,一进来是来读数据的所以上读锁,进来后发现数据是空的,就先把读锁释放再重新获取写锁,就开始写数据,数据写完了,就把写锁释放,把读锁重新挂上,持有读锁时不能同时获取写锁,但拥有写锁时可同时再获取读锁,自己线程挂的写锁可同时挂读锁的,这就是降级,就是除了读锁和写锁外,还有读写锁也叫更新锁,就是自己即可以读又可以写的锁,也就是在自己拥有写锁还没释放写锁时就获取了读锁就降级为读写锁/更新锁,但是不能在持有读锁时再获取写锁;
基于上面的例子,我们可以实现一个缓存系统:
package javaplay.thread.test;import java.util.HashMap;import java.util.Map;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;public class CacheDemo { private Mapcache = new HashMap<>(); public static void main(String[] args) { } // 可做到多个线程并必的读 读和写又互斥 系统性能很高 // 这就是读写锁的价值 private ReadWriteLock rwl = new ReentrantReadWriteLock(); public Object getData(String key) { rwl.readLock().lock(); Object value = null; try { value = cache.get(key); if (value == null) {// 避免首次多次查询要加synchronized rwl.readLock().unlock(); rwl.writeLock().lock(); try { if (value == null) // 就算第二个第三个线程进来时也不用再写了 跟伪代码相同原理 value = "aaa";// 实际去query db } finally { rwl.writeLock().unlock(); } rwl.readLock().lock(); } } finally { rwl.readLock().unlock(); } return value; }}
错误之处:没有把不存在的值put;要用get(key)来判空