ReentrantReadWriteLock读写锁
示例
虽然平常的加锁能保证数据一致性,但是呢,效率很低,你写的时候是加锁了,但是加的太死了:就比如你在写,你不准别人来写没问题,但是如果别人不是来写的只是来读的,你一次只准一个人读,那么效率就会很低下,于是读写锁(允许多个线程同时读,但是如果有一个线程来写共享资源,那么此时其它的读、写线程都应该被阻塞)便出来了。
示例
package cduck
.cn
;
import java
.util
.HashMap
;
import java
.util
.Map
;
import java
.util
.concurrent
.TimeUnit
;
import java
.util
.concurrent
.locks
.ReadWriteLock
;
import java
.util
.concurrent
.locks
.ReentrantReadWriteLock
;
class MyCache{
private volatile Map
<String,Object> map
=new HashMap<>();
private ReadWriteLock readWriteLock
=new ReentrantReadWriteLock();
public void put(String key
,Object value
) throws InterruptedException
{
readWriteLock
.writeLock().lock();
try{
System
.out
.println(Thread
.currentThread().getName()+"\t 开始写入"+key
);
TimeUnit
.MICROSECONDS
.sleep(3);
map
.put(key
, value
);
System
.out
.println(Thread
.currentThread().getName()+"\t 写入完成");
}catch (Exception e
){
}finally {
readWriteLock
.writeLock().unlock();
}
}
public void get(String key
) throws InterruptedException
{
readWriteLock
.readLock() .lock();
try{
System
.out
.println(Thread
.currentThread().getName()+"\t 开始读取");
TimeUnit
.MICROSECONDS
.sleep(3);
Object result
= map
.get(key
);
System
.out
.println(Thread
.currentThread().getName()+"\t 读取完成"+result
);
}catch (Exception e
){
}finally {
readWriteLock
.readLock().unlock();
}
}
}
public class ReadWriteLockDemo {
public static void main(String
[] args
) {
MyCache myCache
=new MyCache();
for (int i
=0;i
<5;i
++){
final int tempInt
=i
;
new Thread(()->{
try {
myCache
.put(tempInt
+"",tempInt
+"");
} catch (InterruptedException e
) {
e
.printStackTrace();
}
},String
.valueOf(i
)).start();
}
for (int i
=0;i
<5;i
++){
final int tempInt
=i
;
new Thread(()->{
try {
myCache
.get(tempInt
+"");
} catch (InterruptedException e
) {
e
.printStackTrace();
}
},String
.valueOf(i
)).start();
}
}
}