StampedLock用法详解
前言
为什么需要用这个锁?已经有了读写锁 ReentrantReadWriteLock ,为什么还需要这个锁?
原因
读写锁 存在一个问题: 当读锁比例很多,写锁很少,锁竞争情况下,写锁抢到锁的机会就回少,读锁数量太大的情况下,写锁不一定能抢到锁。
StampedLock锁使用场景
当读写锁比例相差很大时候,为了均衡读和写不出现锁饥饿现象,特此JDK中引入了一个新的锁,用来防止读写时候出现锁饥饿。
具体原理: 读数据之前会监控是否写锁对其进行了操作(修改),修改了我就去读,没有修改我就不会去读。
StampedLock 读锁之前会判断是否对时间戳进行了,修改 没有修改则不会去读,有修改则去读
具体方法,尝试获取锁,返回一个时间戳
读的时候 根据这个时间戳去判断是否进行了修改
具体使用方法
在读的时候进行判断即可
private static StampedLock stampedLock = new StampedLock();
private static final LinkedList<Long> list = new LinkedList<>();
public static void main(String[] args) {
Runnable writeRunnable = new Runnable() {
@Override
public void run() {
for (; ; ) {
write();
}
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
for (; ; ) {
read();
}
}
};
ExecutorService executorService = Executors.newFixedThreadPool(10);
executorService.submit(runnable);
executorService.submit(runnable);
executorService.submit(runnable);
executorService.submit(runnable);
executorService.submit(runnable);
executorService.submit(runnable);
executorService.submit(runnable);
executorService.submit(runnable);
executorService.submit(runnable);
executorService.submit(writeRunnable);
}
static void read() {
long stamp = stampedLock.tryOptimisticRead();
System.err.println(stamp);
if (stampedLock.validate(stamp)) {
try {
stamp = stampedLock.readLock();
System.out.println(list.stream().map(String::valueOf).collect(Collectors.joining("=", "|", "#")));
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
stampedLock.unlockRead(stamp);
}
}
}
static void write() {
long l = -1;
try {
l = stampedLock.writeLock();
list.addLast(System.currentTimeMillis());
TimeUnit.SECONDS.sleep(1);
System.out.println(Thread.currentThread().getName() + " : W");
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
stampedLock.unlockWrite(l);
}
}
|