论坛首页 Java企业应用论坛

关于在Terracotta中使用ReentrantReadWriteLock

浏览 2075 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-17   最后修改:2009-07-20
ReentrantReadWriteLock 不需要额外的配置tc-config.xml。但是,如果ReentrantReadWriteLock不是包含在一个cluster object里或者它本身不是一个cluster object,需要将它放在一个cluster object的结构里或者直接声明为root. 通常包含ReentrantReadWriteLock的cluster object有CurrentHashMap,HashMap。注意,有些数据结构TC并不支持,比如WeakHashMap.  还有一些数据结构比如CurrentStringMap是TC扩展 http://forge.terracotta.org/releases/projects/tim-concurrent-collections-root/
如果没有把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.
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics