Home  >  Article  >  Java  >  The difference between synchronized and static synchronized

The difference between synchronized and static synchronized

高洛峰
高洛峰Original
2016-12-13 11:18:171310browse

1. The difference between synchronized and static synchronized
Synchronized locks 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", and the two of the class There is no such constraint for different instances. Then static synchronized happens to control access to all instances of the class. Static synchronized restricts threads from accessing all instances of the class in the jvm at the same time and accessing the corresponding code. In fact, if there is synchronized in a certain method or code block in the class, then after generating an instance of the class, the class will have a monitoring block, and the synchronized protection block will be placed to allow threads to access the instance concurrently, while static synchronized will be all Instances of this class share a common monitor, which 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 More" Thread Design Pattern" has such an example:
      pulbic class Something(){
                                                                                      use using pulbic class Something() ‑ ‑ public synchronized void isSyncB(){}
             cSyncB(){}
}
So, if two instances a and b of the Something class are added, then 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()
c. x.cSyncA() and y.cSyncB()
d. x.isSyncA() and Something.cSyncA()
Here, it can be clearly judged:
a. They all access the synchronized domain of the same instance, so they cannot be accessed at the same time. b. They are for different instances, so they can be accessed at the same time. c. Because they are static synchronized, different instances will still be restricted. , 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 synchronzied instance methods and synchronzied class methods have different locks.
Personal analysis is that synchronized and static synchronized are equivalent to two gangs. Each takes care of its own affairs. There is no restriction on each other and can be accessed at the same time. It is not yet clear how Java's internal design synchronzied is implemented.


Conclusion: A: synchronized static is the scope of a certain class, synchronized static cSync{} prevents multiple threads from accessing the synchronized static method 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
There is no difference between synchronized methods(){} and synchronized(this){}, but synchronized methods(){} is easier to read and understand, while synchronized(this) {} can more accurately control the conflict restricted access area, and sometimes performs more efficiently.


3. The synchronized keyword cannot be inherited

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn