ReadWriterLock
public class Test {
public static void main(String[] args) {
MyCache myCache = new MyCache();
for (int i = 1; i <= 5; i++) {
int finalI = i;
new Thread(() -> {
myCache.put(finalI + "", finalI);
}, i + "").start();
}
for (int i = 1; i <= 5; i++) {
int finalI = i;
new Thread(() -> {
myCache.get(finalI + "");
}, i + "").start();
}
}
}
/*
* 1开始写入,key=1
3开始写入,key=3
3写入完成
2开始写入,key=2
2写入完成
1写入完成
4开始写入,key=4
4写入完成
* 可以看到,1写入时,3插队了,
* */
//不使用读写锁,会有插队问题
class MyCache {
private volatile Map<String, Object> map = new HashMap<>();
public void put(String key, Object value) {
System.out.println(Thread.currentThread().getName() + "开始写入,key=" + key);
map.put(key, value);
System.out.println(Thread.currentThread().getName() + "写入完成");
}
public void get(String key) {
System.out.println(Thread.currentThread().getName() + "开始读取,key=" + key);
map.get(key);
System.out.println(Thread.currentThread().getName() + "读取完成");
}
}
//使用读写锁,也可使用synchronized或者ReentrantLock()解决,但读写锁粒度更高
class MyCacheLock {
private volatile Map<String, Object> map = new HashMap<>();
//private ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<>();
private ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public void put(String key, Object value) {
Lock lock = readWriteLock.writeLock();
try {
lock.lock();
System.out.println(Thread.currentThread().getName() + "开始写入,key=" + key);
map.put(key, value);
System.out.println(Thread.currentThread().getName() + "写入完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void get(String key) {
System.out.println(Thread.currentThread().getName() + "开始读取,key=" + key);
map.get(key);
System.out.println(Thread.currentThread().getName() + "读取完成");
}
}