简体   繁体   中英

Lock acquistion on static members within a Java Class

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 ?

他们现在将争用2个对象“ resr1Lock” /“ resrc2Lock。它将按您期望的那样工作。

他们不会再争夺对Class对象的锁定,因此可以解决该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM