[Logo] Terracotta Discussion Forums (LEGACY READ-ONLY ARCHIVE)
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
静态方法上用Autolocks同步  XML
Forum Index -> Terracotta 中文社区
Author Message
eric.sun

jedi

Joined: 10/02/2008 09:18:42
Messages: 123
Offline

假设有如下类, 里面的方法和变量都是静态的,(我们姑且不论这样设计是否合理)

在通常情况下(实例方法同步)锁是加在this instance,这里因为是静态方法,2个方法的同步锁都会加在A.class上,这种情况是否可以用Autolocks,我试了一下好像不行,抛出
om.tc.object.tx.UnlockedSharedObjectException,用namedlock又不符合需求


我的问题是,这样做是否可行,还是必须修改method1,method2使用同步块同步走map 或list锁下
Code:
<root>
     <field-name>A.map</field-name>
 </root>
 <root>
     <field-name>A.list</field-name>
 </root>
 <autolock auto-synchronized="true">
           <method-expression>void A.method1()</method-expression>
 <lock-level>write</lock-level>
 </autolock>
 <autolock auto-synchronized="true">
           <method-expression>void A.method2()</method-expression>
 <lock-level>write</lock-level>
 </autolock>
 
 



Code:
public class A{
 
 private static Map map = new ConcurrentHashMap();
 private static List list = Collections.synchronizedList(new LinkedList());
 
 public static synchronized void method1() {
  map.put();
  list.add();
 }
 public static synchronized void method2() {
  map.remove()
  list.remove();
 }
 }



谢谢
zeeiyer

consul

Joined: 05/24/2006 14:28:28
Messages: 493
Offline

I take it the question here is why the UnlockedSharedException?

The rules are that the object you are locking on also needs to be shared.

It seems that in this case, when you auto-lock on m1 and m2 with method level synchronization, you are locking on an instance of A, which is not shared (A.map, A.list and their references are shared, but A itself isn't) - hence the UnLockSharedException.

Many fixes possible - example - you could add to Class A:
Code:
 private static ReentrantReadWriteLock rrwl = new ReentrantReadWriteLock();
 

Add A.rrwl as a root. & then in method m1 and m2, wrap the operations against map and list with a rrwl.writeLock()
Thanks


Sreeni Iyer, Terracotta.
Not a member yet - Click here to join the Terracotta Community
eric.sun

jedi

Joined: 10/02/2008 09:18:42
Messages: 123
Offline

Thanks Sreeni !

In that case, it doesn't has to be a ReentrantReadWriteLock instance, right ? It can be any object instance, for example if I make v as a root, i can do this:
Code:
 
 public class A{
  
 private static Map map = new ConcurrentHashMap();
  private static List list = Collections.synchronizedList(new LinkedList());
  private static Vector v = new Vector();
  
  public static void method1() {
  synchronized (v){
   map.put();
   list.add();
   }
  }
  public static void method2() {
  synchronized (v){
   map.remove()
   list.remove();
   }
  }
  }


lima

consul
[Avatar]
Joined: 06/22/2009 10:12:31
Messages: 361
Offline

使用RRWL的好处是,当有大批量读写访问的时候,并行性达到最高。

对于你前面提交的为代码来说,甚至用一个Object对象就够了。连Vector应该都不需要。

另外,有点好奇,不知道把A.class做为root行不行。
eric.sun

jedi

Joined: 10/02/2008 09:18:42
Messages: 123
Offline

另外,有点好奇,不知道把A.class做为root行不行。 

对,实际上这个是我最想问的。因为我主观上以为Terracotta会自动处理静态方法,就像对待instance方法一样,会自动在A.class上加上同步锁,但是我试了一下好像不成功。
zeeiyer

consul

Joined: 05/24/2006 14:28:28
Messages: 493
Offline

Right it could work with a Vector etc. - With a RRWL though, you don't need the autolock section in the tc-config.xml at all - nor do you need the old-style synchronization blocks. You just need to cluster the RRWL and use RRWL read or write locks as appropriate.
thanks

Sreeni Iyer, Terracotta.
Not a member yet - Click here to join the Terracotta Community
 
Forum Index -> Terracotta 中文社区
Go to:   
Powered by JForum 2.1.7 © JForum Team