SoFunction
Updated on 2025-05-17

The difference and description of Synchronized and Static Synchronized in Java

The difference between Synchronized and Static Synchronized

synchronizedIt 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"Current instance of class", there is no such constraint on two different instances of the class.

Sostatic synchronizedIt just happens that you want to control access to all instances of the class.static synchronizedIt restricts threads to access all instances of this class in jvm at the same time and access the corresponding code blocks.

A Japanese author - "Java Multithreaded Design Pattern" by Jie Chenghao

A column

pulbic class Something(){
    public synchronized void isSyncA(){}
    public synchronized void isSyncB(){}
    public static synchronized void cSyncA(){}
    public static synchronized void cSyncB(){}

Then, add two instances a and b of Something class

Why can the following group methods be accessed simultaneously by more than one thread?

a. ()and() 
b. ()and()
c. ()and()
d. ()and()
  • a. They are all synchronized domains of the same instance, so they cannot be accessed simultaneously
  • b. is for different instances, so it can be accessed at the same time
  • c. Because it isstatic synchronized, so different instances will still be restricted, which is equivalent to()and(), so it cannot be accessed simultaneously.
  • d. can be accessed simultaneously, because synchronzied's instance method and synchronzied's class method are different from those of locking.

Let's give a 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 a 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.

Summarize

There are several points as follows:

  1. The object lock key can only be mutually exclusive and ensure the uniqueness of shared variables.
  2. The locks on static methods are not the same as those on instance methods by default. If synchronized, two locks need to be formulated.
  3. Regarding the locks on methods of the same class, they come from the object that calls the method. If the object that calls the method is the same, the locks must be the same, otherwise they will be different. for examplenew A().x()andnew A().x(),The objects are different and the locks are different. If A is single-interest, it can be mutually exclusive.
  4. Static method locking can be mutually exclusive with all other static method locking.
  5. Static method locking, like locking, is directly in the class.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.