This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CacheData { | |
Object data; | |
volatile boolean isCacheValid; | |
final ReadWriteLock rwl = new ReentrantReadWriteLock(); | |
// 读锁 | |
final Lock r = rwl.readLock(); | |
// 写锁 | |
final Lock w = rwl.writeLock(); | |
void processCacheData() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author luxz | |
* @date 2020/6/27-3:31 AM | |
* 如果执行乐观读操作期间,存在写操作,会把乐观读升级为悲观读锁 | |
*/ | |
public class Point { | |
private int x, y; | |
final StampedLock sl = new StampedLock(); | |
int distanceFromOrigin() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GuardedObject<T> { | |
// 受保护的对象 | |
T obj; | |
final Lock lock = new ReentrantLock(); | |
final Condition done = lock.newCondition(); | |
final int timeout = 1; | |
T get(Predicate<T> p) { | |
lock.lock(); | |
try{ |
OlderNewer