views:

53

answers:

2

Hi,

I am currently resolving a performance degradation issue due to heavy lock contention. I am considering "Lock splitting" to resolve this issue.

The skeletal usage pattern is ::

CURRENT USAGE ::

public class HelloWorld{

   public static synchronized method1(){
       //uses resource 1
   }
   public static synchronized method2(){
        //uses resource 2
   }

}

MY APPROACH ::

since method1() and method2() does not use the same resource, I am thinking of splitting the locks. As of now, they both contend for the Class lock since they are both static synchronized. I am thinking of changing it to ::

public class HelloWorld{

   **private static Object resr1Lock = new Object();**

   public static method1(){
       synchronized(resrc1Lock){
            //uses resource 1
       }
   }

   **private static Object resr2Lock = new Object();** 
   public static method2(){
        synchronized(resrc2Lock){
             //uses resource 2
        }
   }

}

Will they now contend for the "Class Lock" or resr1Lock/resrc2Lock ?

+3  A: 

They will now contend for the 2 objects "resr1Lock"/"resrc2Lock. It will work as you expect.

Manuel Selva
+2  A: 

They won't contend for a lock on the Class object anymore so yes this will solve that problem.

cletus