Java 中的锁 ReentrantReadWriteLock 读写锁
Java juc 锁 About 3,844 words读写锁
读读不互斥,读写互斥,写写互斥。
代码示例
public class ReadWriteLockDemo {
public static void main(String[] args) {
MyCache myCache = new MyCache();
for (int i = 1; i <= 5; i++) {
int finalI = i;
new Thread(() -> {
/*try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
myCache.put(finalI +"", finalI +"");
}, String.valueOf(i)).start();
}
for (int i = 1; i <= 5; i++) {
int finalI = i;
new Thread(() -> {
myCache.get(finalI +"");
}, String.valueOf(i)).start();
}
}
private static class MyCache {
private volatile Map<String, Object> map = new HashMap<>();
private ReadWriteLock lock = new ReentrantReadWriteLock();
public void put(String key, String value) {
lock.writeLock().lock();
try {
System.out.println(LocalDateTime.now() + "\t" + Thread.currentThread().getName() + "\t正在写入:" + key);
// 模拟网络延迟
try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); }
map.put(key, value);
System.out.println(LocalDateTime.now() + "\t" + Thread.currentThread().getName() + "\t写入完成:" + key);
} finally {
lock.writeLock().unlock();
}
}
public void get(String key) {
lock.readLock().lock();
try {
System.out.println(LocalDateTime.now() + "\t" + Thread.currentThread().getName() + "\t正在读取:" + key);
// 模拟网络延迟
/* try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
Object result = map.get(key);
System.out.println(LocalDateTime.now() + "\t" + Thread.currentThread().getName() + "\t读取完成:" + key + "-" + result);
} finally {
lock.readLock().unlock();
}
}
}
}
main()
方法中休眠一秒注释掉,输出:
2021-03-27T22:22:36.823 1 正在写入:1
2021-03-27T22:22:37.135 1 写入完成:1
2021-03-27T22:22:37.135 2 正在写入:2
2021-03-27T22:22:37.437 2 写入完成:2
2021-03-27T22:22:37.437 3 正在写入:3
2021-03-27T22:22:37.741 3 写入完成:3
2021-03-27T22:22:37.741 5 正在写入:5
2021-03-27T22:22:38.047 5 写入完成:5
2021-03-27T22:22:38.047 4 正在写入:4
2021-03-27T22:22:38.358 4 写入完成:4
2021-03-27T22:22:38.358 1 正在读取:1
2021-03-27T22:22:38.358 1 读取完成:1-1
2021-03-27T22:22:38.358 2 正在读取:2
2021-03-27T22:22:38.358 2 读取完成:2-2
2021-03-27T22:22:38.358 3 正在读取:3
2021-03-27T22:22:38.358 3 读取完成:3-3
2021-03-27T22:22:38.366 5 正在读取:5
2021-03-27T22:22:38.366 5 读取完成:5-5
2021-03-27T22:22:38.366 4 正在读取:4
2021-03-27T22:22:38.366 4 读取完成:4-4
main()
方法中休眠一秒打开,输出:
2021-03-27T22:20:52.218 1 正在读取:1
2021-03-27T22:20:52.218 5 正在读取:5
2021-03-27T22:20:52.219 1 读取完成:1-null
2021-03-27T22:20:52.218 3 正在读取:3
2021-03-27T22:20:52.219 3 读取完成:3-null
2021-03-27T22:20:52.218 4 正在读取:4
2021-03-27T22:20:52.219 4 读取完成:4-null
2021-03-27T22:20:52.218 2 正在读取:2
2021-03-27T22:20:52.219 5 读取完成:5-null
2021-03-27T22:20:52.219 2 读取完成:2-null
2021-03-27T22:20:53.174 1 正在写入:1
2021-03-27T22:20:53.474 1 写入完成:1
2021-03-27T22:20:53.474 5 正在写入:5
2021-03-27T22:20:53.776 5 写入完成:5
2021-03-27T22:20:53.776 2 正在写入:2
2021-03-27T22:20:54.085 2 写入完成:2
2021-03-27T22:20:54.085 3 正在写入:3
2021-03-27T22:20:54.389 3 写入完成:3
2021-03-27T22:20:54.389 4 正在写入:4
2021-03-27T22:20:54.699 4 写入完成:4
Views: 1,883 · Posted: 2021-04-08
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...