赞
踩
读写锁说明
再说说演变
读写锁意义和特点
可重入
读写兼顾
结论:一体两面,读写互斥,读读共享,读没有完成的时候其他线程写锁无法获得
锁降级:
StampedLock是JDK1.8中新增的一个读写锁,也是对JDK1.5中的读写锁ReentrantReadWriteLock的优化
stamp 代表了锁的状态。当stamp返回零时,表示线程获取锁失败,并且当释放锁或者转换锁的时候,都要传入最初获取的stamp值。
锁饥饿问题:
如何解决锁饥饿问题:
所有获取锁的方法,都返回一个邮戳,stamp为零表示失败,其余都表示成功
所有释放锁的方法,都需要一个邮戳,这个stamp必须是和成功获取锁时得到的stamp一致
StampedLock是不可重入的,危险(如果一个线程已经持有了写锁,在去获取写锁的话会造成死锁)
StampedLock有三种访问模式:
一句话:读的过程中也允许写锁介入
public class StampedLockDemo { static int number = 37; static StampedLock stampedLock = new StampedLock(); public void write() { long stamp = stampedLock.writeLock(); System.out.println(Thread.currentThread().getName() + "\t" + "写线程准备修改"); try { number = number + 13; } finally { stampedLock.unlockWrite(stamp); } System.out.println(Thread.currentThread().getName() + "\t" + "写线程结束修改"); } public void read() { long stamp = stampedLock.readLock(); System.out.println(Thread.currentThread().getName() + "\t" + " come in readLock codeBlock"); for (int i = 0; i < 4; i++) { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "\t" + " 正在读取中"); } try { int result = number; System.out.println(Thread.currentThread().getName() + "\t" + "获得成员变量值result: " + result); System.out.println("写线程没有修改成功,读锁时候写锁无法介入,传统的读写互斥"); } finally { stampedLock.unlockRead(stamp); } } public static void main(String[] args) { StampedLockDemo resource = new StampedLockDemo(); new Thread(() -> { resource.read(); }, "readThread").start(); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() -> { System.out.println(Thread.currentThread().getName()+"\t"+" come in"); resource.write(); }, "writeThread").start(); } } /** * readThread come in readLock codeBlock * readThread 正在读取中 * writeThread come in * readThread 正在读取中 * readThread 正在读取中 * readThread 正在读取中 * readThread 获得成员变量值result: 37 * 写线程没有修改成功,读锁时候写锁无法介入,传统的读写互斥 * writeThread 写线程准备修改 * writeThread 写线程结束修改 */
public class StampedLockDemo { static int number = 37; static StampedLock stampedLock = new StampedLock(); public void write() { long stamp = stampedLock.writeLock(); System.out.println(Thread.currentThread().getName() + "\t" + "写线程准备修改"); try { number = number + 13; } finally { stampedLock.unlockWrite(stamp); } System.out.println(Thread.currentThread().getName() + "\t" + "写线程结束修改"); } public void read() { long stamp = stampedLock.tryOptimisticRead(); int result = number; System.out.println("4秒前 stampedLock.validate方法值(true 无修改 false有修改)" + "\t" + stampedLock.validate(stamp)); for (int i = 0; i < 4; i++) { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "\t" + " 正在读取...." + i + "秒后" + "stampedLock.validate方法值(true 无修改 false有修改)" + "\t" + stampedLock.validate(stamp)); } if (!stampedLock.validate(stamp)) { System.out.println("有人修改----------有写操作"); stamp = stampedLock.readLock(); try { System.out.println("从乐观读升级为悲观读"); result = number; System.out.println("重新悲观读后result:" + result); } finally { stampedLock.unlockRead(stamp); } } System.out.println(Thread.currentThread().getName() + "\t" + "finally value: " + result); } public static void main(String[] args) { StampedLockDemo resource = new StampedLockDemo(); new Thread(() -> { resource.read(); }, "readThread").start(); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(() -> { System.out.println(Thread.currentThread().getName() + "\t" + " come in"); resource.write(); }, "writeThread").start(); } } /** * 4秒前 stampedLock.validate方法值(true 无修改 false有修改) true * readThread 正在读取....0秒后stampedLock.validate方法值(true 无修改 false有修改) true * readThread 正在读取....1秒后stampedLock.validate方法值(true 无修改 false有修改) true * writeThread come in * writeThread 写线程准备修改 * writeThread 写线程结束修改 * readThread 正在读取....2秒后stampedLock.validate方法值(true 无修改 false有修改) false * readThread 正在读取....3秒后stampedLock.validate方法值(true 无修改 false有修改) false * 有人修改----------有写操作 * 从乐观读升级为悲观读 * 重新悲观读后result:50 * readThread finally value: 50 */
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。