1. Differences between synchronized and static synchronized
Synchronized locks the current instance of the class to prevent other threads from simultaneously accessing all the synchronized blocks of the instance of the class. Note that this is the "current instance of the class ", there is no such constraint for two different instances of the class. Static synchronized is to control the access to all instances of the class. Static synchronized is to restrict the thread to access all instances of the class in JVM at the same time and access the corresponding code quickly. In fact, synchronized exists in a method or code block in the class. After a class instance is generated, the change of the class will have a fast monitoring speed, and the protection of the Instance synchronized will be fast when concurrent thread access is placed, static synchronized is a public monitoring speed for all instances of this class, that is, the two differences, that is, synchronized is equivalent to this. synchronized, and
Static synchronized is equivalent to something. synchronized.
A Japanese author, "Java multi-thread design mode", has the following column:
Pulbic class something (){
Public synchronized void issynca (){}
Public synchronized void issyncb (){}
Public static synchronized void csynca (){}
Public static synchronized void csyncb (){}
}
So, if two instances A and B are added to the something class, why are the following methods simultaneously accessed by more than one thread?
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, we can clearly judge:
A, All accesses to the synchronized domain of the same instance, so they cannot be accessed at the same time
B. It is for different instances and thus can be accessed at the same time.
C. Because it is static synchronized, different instances are still limited. It is 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 is that it can be accessed at the same time. The reason is that synchronzied is because the instance method and synchronzied class method are different from lock.
Personal analysis, that is, synchronized and static synchronized, are equivalent to two gangs. Each of them is independent of each other and can be accessed at the same time. It is not clear how synchronzied is implemented in Java internal design.
Conclusion: A: Synchronized static is the scope of a class. Synchronized static csync {} prevents multiple threads from simultaneously accessing the synchronized static method in this class. It can work on all object instances of the class.
B: Synchronized is the range of an instance. Synchronized issync () {} prevents multiple threads from simultaneously accessing the Synchronized Method in this instance.
2. Fast difference between the Synchronized Method and synchronized code
Synchronized methods () {} is no different from synchronized (this) {}, but synchronized methods () {} is easier to read and understand, while synchronized (this) {} can more accurately control access areas restricted by conflict, sometimes more efficient.
3. the synchronized keyword cannot be inherited.
This is
Differences between synchronized and static synchronized