The difference between synchronized and static synchronized

  1. The difference between synchronized and static synchronized
      Synchronized is to lock the current instance of the class to prevent other threads from accessing all synchronized blocks of the instance of the class at the same time. Note that here is "the current instance of the class", two different instances of the class There is no such restriction. Then static synchronized is exactly to control the access of all instances of the class. Static synchronized is to restrict threads from accessing all instances of the class in the jvm at the same time and accessing the corresponding code faster. In fact, if there is synchronized in a method or a code block in a class, then after generating an instance of this class, there will be a monitoring speed for changing the class, and placing threads concurrently accessing the instance is synchronized to protect the speed, while static synchronized is all Instances of this class share a common monitor, that is, the difference between the two, that is, synchronized is equivalent to this.synchronized, and
static synchronized is equivalent to Something.synchronized.
      A Japanese author-Jie Chenghao's "java multithreading design" Pattern" has such an example:
      pulbic class Something(){
         public synchronized void isSyncA(){}
         public synchronized void isSyncB(){}
         public static synchronized void cSyncA(){}
         public static synchronized void cSyncB(){}
     }
   Then, if two instances a and b of the Something class are added, why are the following group methods accessed by more than one thread at the same time?
   a. x.isSyncA() and x.isSyncB()    b. x.isSyncA() and y.isSyncA()    cxcSyncA( ) and y.cSyncB()    d. x.isSyncA() and Something.cSyncA()     Here, it can be clearly judged that:    a, both access the synchronized domain of the same instance, so they cannot be accessed at the same time    b, are for different instances , so c can be accessed at the same time    , because it is static synchronized, so different instances will still be limited, equivalent to Something.isSyncA() and Something.isSyncB(), so they cannot be accessed at the same time.    So, what about d?, the answer in the book can be accessed at the same time. The reason for the answer is that instance methods are synchronzied and class methods are synchronzied due to different reasons for locking.    Personal analysis is that synchronized and static synchronized are equivalent to two factions. It is still unclear how the internal design of java is synchronzied. 









    Conclusion : A: synchronized static is the scope of a class, synchronized static cSync{} prevents multiple threads from accessing synchronized static methods in this class at the same time. It works on all object instances of the class.                B: synchronized is the scope of an instance, synchronized isSync(){} prevents multiple threads from accessing the synchronized method in this instance at the same time.
   



     2. The difference between synchronized methods and synchronized code is fast There is no difference between
      
synchronized methods(){} and synchronized(this){}, but
synchronized methods(){} is easier to read and understand, while synchronized(this){} can be more precise The control violation restricts access to the area, which is sometimes more efficient. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326136073&siteId=291194637