如果没有把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.