Java类中静态成员的锁获取

Java类中静态成员的锁获取,java,concurrency,static-members,synchronized,Java,Concurrency,Static Members,Synchronized,我目前正在解决由于严重的锁争用而导致的性能下降问题。我正在考虑使用锁拆分来解决此问题 骨架使用模式为: 当前使用情况:: public class HelloWorld{ public static synchronized method1(){ //uses resource 1 } public static synchronized method2(){ //uses resource 2 } } 我的方法是: 由于method1

我目前正在解决由于严重的锁争用而导致的性能下降问题。我正在考虑使用锁拆分来解决此问题

骨架使用模式为:

当前使用情况::

public class HelloWorld{

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

}
我的方法是:

由于method1和method2不使用相同的资源,因此我考虑拆分锁。到目前为止,它们都在争夺类锁,因为它们都是静态同步的。我想把它改成::

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
        }
   }

}

他们现在会争夺类锁还是resr1Lock/resrc2Lock

他们现在将争夺两个对象resr1Lock/resrc2Lock。它将如您所期望的那样工作。

他们将不再争夺类对象的锁,因此是的,这将解决该问题。

非常感谢,伙计们。它确实提高了性能:-[提高了近20倍]。分析任何应用程序中的同步模式的另一个引人注目的案例研究既然method1和method2不使用共享资源,为什么还要使用同步?