简体   繁体   English

同步方法与块的性能

[英]performance of synchronized method vs block

I've a class where all methods need to be synchronized (no static method). 我有一个类,所有方法都需要同步(没有静态方法)。 One of those method will be called once every 50 ms. 其中一种方法将每50 ms调用一次。

I'm wondering where putting the synchronized keyword to have the shortest execution time ? 我想知道在哪里放置synchronized关键字有最短的执行时间?

ie Is there any differences between the 2 options detailed bellow regarding execution time ? 即关于执行时间的2个选项之间是否有任何差异?

Option 1 (synchronized methods) 选项1(同步方法)

public class MyNativeObject{

     private boolean released = false;

     public synchronized String read(){
         if(released) return null;
         return Read(); 
     }

     public synchronized void release(){
         if(released) return;
         released = true;
         Release();
     }
     private native String Read();
     private native void Release();
}

Option 2 (synchronized block) 选项2(同步块)

public class MyNativeObject{

     private Boolean released = false;

     public String read(){
         synchronized(released){
             if(released) return null;
             return Read(); 
         }
     }

     public void release(){
         synchronized(released){
             if(released) return;
             released = true;
             Release();
         }
     }
     private native String Read();
     private native void Release();
}

Option #1 works, and is almost perfect, except that the synchronized-on object is visible to the outside, which should be avoided if possible. 选项#1工作,并且几乎是完美的,除了同步对象对外部可见,如果可能应该避免。

Option #2 is very dangerous and probably wrong, as the object you synchronize on changes (when released changes from Boolean.TRUE to Boolean.FALSE it will synchronize on another object! 选项#2是非常危险的,可能是错误的,因为您同步的对象发生了更改(当releasedBoolean.TRUE更改为Boolean.FALSE它将在另一个对象上同步!

Option #3 would be this: 选项#3将是这样的:

public class MyNativeObject{

     private boolean released = false;
     private final Object monitor = new Object[0];

     public String read(){
         synchronized(monitor){
             if(released) return null;
             return Read(); 
         }
     }

     public void release(){
         synchronized(monitor){
             if(released) return;
             released = true;
             Release();
         }
     }
     private native String Read();
     private native void Release();
}

The idea is to use an object as the monitor that's not accessible from outside your code (see this page to read about my using a new Object[0] in place of the more common new Object() ). 我们的想法是使用一个对象作为无法从代码外部访问的监视器(请参阅此页面以了解我使用new Object[0]代替更常见的new Object() )。 This way, no other synchronization can interfere with your synchronization. 这样,没有其他同步会干扰您的同步。

You can't have a synchronized class. 你不能有一个同步的类。

As Joachim points out, Option 2 is wrong as you are locking on a) a field which changes and b) a global object. 正如约阿希姆指出的那样,选项2是错误的,因为你锁定a)一个改变的领域和b)一个全局对象。

The synchronized methods is not only the only one which works but the simplest of those which would so I would use that. 同步方法不仅是唯一有效的方法,而且是最简单的方法,我将使用它。

A typical lock takes around 50 nano-seconds, so if you call these methods every 50,000,000 nano-seconds, it is highly unlikely that it will make any difference. 典型的锁定需要大约50纳秒,所以如果你每50,000,000纳秒调用这些方法,它就不太可能产生任何差别。

public class MyNativeObject{

     private boolean released = false;

     public synchronized String read(){
         return released ? null : Read(); 
     }

     public synchronized void release(){
         if(released) return;
         released = true;
         Release();
     }

     // make the native methods static if you can.
     private static native String Read();
     private static native void Release();
}

Synchronized methods are just syntactic sugar! 同步方法只是语法糖!

These two are equivalent: 这两个是等价的:

 public synchronized String read(){
      return released ? null : Read(); 
 }

 public String read(){
      synchronized(this) {
           return released ? null : Read(); 
      }
 }

Hence there will be no performance improvement. 因此,没有性能改进。

"Manual" ( synchronized() ) synchronization makes sense if either is true: 如果其中任何一个为真,“手动”( synchronized() )同步是有意义的:

  • there are different critical sections which may depend on a different lock 有不同的关键部分可能取决于不同的锁

  • critical section may be shorter than the method 临界区可能比方法短

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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