Java高并发编程中StampedLock的使用及详细介绍-刘宇
作者:刘宇
CSDN博客地址:https://blog.csdn.net/liuyu973971883
有部分资料参考,如有侵权,请联系删除。如有不正确的地方,烦请指正,谢谢。
一、什么是StampedLock
他基本包含了ReentrantLock和ReentrantReadWriteLock的所有方法,是对ReentrantReadWriteLock锁的增强方法。他的出现是要解决ReentrantReadWriteLock在读写分离时的线程饥饿问题。当ReentrantReadWriteLock对其写锁想要获取的话,就必须没有任何其他读写锁存在才可以,这实现了悲观读取。如果读操作很多,写很少的情况下,线程有可能就会遭遇饥饿问题,而StampedLock正是解决了这个问题,优化了读锁和写锁之间的相互转换。
二、StampedLock的三种模式
- 1、写入(Writing):writeLock是一个独占锁,也是一个悲观锁。
- 2、读取(Reading):readLock这时候是一个悲观锁。
- 3、乐观读取(Optimistic Reading):提供了tryOptimisticRead方法返回一个非0的stamp,只有当前同步状态没有被写模式所占有是才能获取到。他是在获取stamp值后对数据进行读取操作,最后验证该stamp值是否发生变化,如果发生变化则读取无效,代表有数据写入。这种方式能够降低竞争和提高吞吐量。
三、案例
package com.brycen.concurrency03.lock;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.StampedLock;
public class StampedLockExample {
final static StampedLock lock = new StampedLock();
final static List<Long> list = new ArrayList<Long>();
public static void main(String[] args) throws InterruptedException {
//创建线程池
ExecutorService executorService = Executors.newFixedThreadPool(10);
//写任务
Runnable writeTask = new Runnable() {
@Override
public void run() {
for (;;) {
write();
}
}
};
//读任务
Runnable readTask = new Runnable() {
@Override
public void run() {
for (;;){
read();
}
}
};
//提交9个读任务,1个写任务
executorService.submit(readTask);
executorService.submit(readTask);
executorService.submit(readTask);
executorService.submit(readTask);
executorService.submit(readTask);
executorService.submit(readTask);
executorService.submit(readTask);
executorService.submit(readTask);
executorService.submit(readTask);
executorService.submit(writeTask);
}
public static void write() {
long stamped = -1;
try {
//获取写锁
stamped = lock.writeLock();
long current = System.currentTimeMillis();
list.add(current);
System.out.println("W-> "+current);
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//释放写锁
lock.unlockWrite(stamped);
}
}
public static void read() {
//获取乐观锁,并返回stamp值,该方法不会使writeLock阻塞
long stamp =lock.tryOptimisticRead();
//读取数据
System.out.println("normal R-> "+stamp+"=="+list.size());
//判断stamp值是否发生变化
if (!lock.validate(stamp)) {
//内容被修改,重新获取
try {
stamp = lock.readLock();
System.out.println("data change R-> "+stamp+"=="+list.size());
TimeUnit.MILLISECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlockRead(stamp);
}
}
}
}
运行结果:
normal R-> 256==0
normal R-> 256==0
normal R-> 256==0
normal R-> 256==0
normal R-> 256==0
normal R-> 256==0
normal R-> 256==0
W-> 1596960390456
W-> 1596960390469
W-> 1596960390481
data change R-> 1026==3
data change R-> 1028==3
data change R-> 1031==3
data change R-> 1027==3
data change R-> 1025==3
data change R-> 1033==3
data change R-> 1032==3
data change R-> 1030==3
data change R-> 1029==3
normal R-> 1024==3
normal R-> 1024==3
normal R-> 1024==3
normal R-> 1024==3