关于在Terracotta中使用ReentrantReadWriteLock

ReentrantReadWriteLock 不需要额外的配置tc-config.xml。但是,如果ReentrantReadWriteLock不是包含在一个cluster object里或者它本身不是一个cluster object,需要将它放在一个cluster object的结构里或者直接声明为root. 通常包含ReentrantReadWriteLock的cluster object有CurrentHashMap,HashMap。注意,有些数据结构TC并不支持,比如WeakHashMap. 还有一些数据结构比如CurrentStringMap是TC扩展 [url]http://forge.terracotta.org/releases/projects/tim-concurrent-collections-root/[/url]
如果没有把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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章