//写入方法
public void writer(String key, String value) {
try {
w.lock();
System.out.println("写入缓存开始:" + key + value);
cache.put(key, value);
System.out.println("写入缓存结束" + key + value);
countDownLatch.countDown();
} catch (Exception e) {
e.printStackTrace();
} finally {
w.unlock();
}
}
//问题:一个线程已经countDown了,await线程发现不了。
//读取方法
public String reader(String key) {
try {
r.lock();
System.out.println(countDownLatch.getCount());
countDownLatch.await();
System.out.println("写入线程已结束");
System.out.println("获取缓存开始" + key);
String value = cache.get(key);
System.out.println("获取缓存结束" + key);
return value;
} catch (InterruptedException e) {
return null;
} finally {
r.unlock();
}
}