概述
适用场景:并发读写场景,且读多写少
为什么要用:提高读场景的性能,对应读远远大于写的场景,性能优化显著
性能提升原因:同步锁或者重入锁在读写时都锁定,并发访问实际是串行处理。改用读写锁后,仅在写操作会锁定并阻塞,并发线程访问仅有读操作时不堵塞
读 | 写 | |
读 | 不阻塞 | 阻塞 |
写 | 阻塞 | 阻塞 |
测试代码
public class ReadThread implements Runnable {
Resources resources;
Lock lock;
public ReadThread(Resources resources,Lock lock) {
this.resources=resources;
this.lock=lock;
}
public void run() {
int value=0;
try {
value = resources.handleRead(lock);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss:SSS")+ "]---read value="+value);
}
}
public class WriteThread implements Runnable {
Resources resources;
Lock lock;
public WriteThread(Resources resources,Lock lock) {
this.resources=resources;
this.lock=lock;
}
public void run() {
try {
resources.handleWirte(lock);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class Resources {
private int value = 0;
public int handleRead(Lock lock) throws InterruptedException {
try {
lock.lock();
Thread.sleep(10);
return this.value;
} finally {
lock.unlock();
}
}
public void handleWirte(Lock lock) throws InterruptedException {
try {
lock.lock();
int newValue = Math.abs(new Random().nextInt() % 10 + 2);
int old = this.value;
this.value = newValue;
String currTime=DateUtils.getCurrentTime("yyyy-MM-dd HH:mm:ss:SSS");
System.out.println(currTime+ "]-old=" + old + ",newValue=" + value);
Thread.sleep(50);
} finally {
lock.unlock();
}
}
}
public static void main(String[] args) {
ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
Lock lock = new ReentrantLock();
Lock readLock = readWriteLock.readLock();
Lock writeLock = readWriteLock.writeLock();
Resources rs = new Resources();
ExecutorService es = Executors.newFixedThreadPool(100);
//普通同步锁
for (int i = 0; i < 10; i++) {
es.submit(new ReadThread(rs, lock));//普通同步锁
}
for (int i = 0; i < 2; i++) {
es.submit(new WriteThread(rs, lock));//普通同步锁
}
for (int i = 0; i < 10; i++) {
es.submit(new ReadThread(rs, lock));//普通同步锁
}
//读写锁
/* for (int i = 0; i < 10; i++) {
es.submit(new ReadThread(rs, readLock)); //读写分离锁
}
for (int i = 0; i < 2; i++) {
es.submit(new WriteThread(rs, writeLock));//读写分离锁
}
for (int i = 0; i < 10; i++) {
es.submit(new ReadThread(rs, readLock)); //读写分离锁
}*/
}
前置条件
1. 先启动10个读线程,接着启动2个写线程,最后启动10个读线程
2.每个读线程获取锁后,睡眠10ms模拟读过程
3.每个写线程获取锁后,先写,再睡眠50ms,再释放锁
结果对比(已剔除写耗时)
同步锁,读总耗时:286ms
2018-12-07 21:21:34:147]---read value=0
2018-12-07 21:21:34:147]---read value=0
2018-12-07 21:21:34:147]---read value=0
2018-12-07 21:21:34:162]---read value=0
2018-12-07 21:21:34:178]---read value=0
2018-12-07 21:21:34:194]---read value=0
2018-12-07 21:21:34:209]---read value=0
2018-12-07 21:21:34:225]---read value=0
2018-12-07 21:21:34:240]---read value=0
2018-12-07 21:21:34:256]---read value=0
2018-12-07 21:21:34:256]-old=0,newValue=1
2018-12-07 21:21:34:319]-old=1,newValue=2
2018-12-07 21:21:34:397]---read value=2
2018-12-07 21:21:34:412]---read value=2
2018-12-07 21:21:34:428]---read value=2
2018-12-07 21:21:34:444]---read value=2
2018-12-07 21:21:34:459]---read value=2
2018-12-07 21:21:34:475]---read value=2
2018-12-07 21:21:34:491]---read value=2
2018-12-07 21:21:34:506]---read value=2
2018-12-07 21:21:34:522]---read value=2
2018-12-07 21:21:34:533]---read value=2
读写锁,读总耗时:34ms
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]-old=0,newValue=4
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:815]---read value=0
2018-12-07 21:22:48:877]-old=4,newValue=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
2018-12-07 21:22:48:949]---read value=10
最后
以上就是矮小毛豆为你收集整理的读写锁ReadWriteLock的全部内容,希望文章能够帮你解决读写锁ReadWriteLock所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复