關於在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.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章