如果沒有把ReentrantReadWriteLock放在這樣的cluster object裏,那麼ReentrantReadWriteLock所起的作用只是local lock. 不會起cluster lock的作用。
一個使用的示例:
private final ConcurrentStringMap<Object> locks = new ConcurrentStringMap<Object>();
private final Object LOCK = new Object();
public void writeOperation(Entity entity) {
if (locks.putIfAbsent(entity.getId(), LOCK) != null) {
//some other thread/node already has lock, exit
return;
}
try {
//critical section write operation here
} finally {
locks.removeNoReturn(entity.getId());
}
}
原文:The only requirement is that any ReentrantReadWriteLock requiring clustered behavior must be a part of the clustered graph.A ReentrantReadWriteLock that is not a part of the clustered graph imparts local locking semantics only.