1、用法
//初始化讀鎖和寫鎖
ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
ReentrantReadWriteLock.ReadLock rlock = rwl.readLock();
ReentrantReadWriteLock.WriteLock wlock = rwl.writeLock();
//加解鎖
rlock.lock();
wlock.lock();
常見的用於cache一類讀多寫少的場景。使用ReentrantReadWriteLock。
2、原理。
readLock支持多線程同時獲取鎖。
writeLock與其他鎖互斥。
參考這個帖子,不在贅述。
https://blog.csdn.net/yanyan19880509/article/details/52435135
https://blog.csdn.net/prestigeding/article/details/53286756