What is the difference between using synchronized to decorate static methods and non static methods

preface

Recently, I was asked this question, and the first answer was also very bad. Here, I refer to the online answers for sorting and recording. For your reference.

Synchronized modifies non static methods

Synchronized modifies a non static method. In fact, it locks the object calling the method, commonly known as "object lock".

Every object in Java has a lock and is unique. Suppose there are multiple methods in an allocated object space, which is equivalent to multiple small rooms in the space. If we lock all small rooms, because the object has only one key, only one person can open a small room at the same time, and then return it after use, and then the JVM will allocate the next person who obtains the key.

Case 1: the same object accesses two synchronization methods of the object in two threads

Result: mutual exclusion occurs.

Explanation: because the lock is for an object, when an object calls a synchronized method, other synchronized methods need to wait for its execution to end and release the lock before execution. As explained above, there is only one key for an object. The two synchronization methods are two rooms. Therefore, only one key can access one method at the same time.

Situation 2: different objects call the same synchronization method in two threads.

Result: mutual exclusion is not generated.

Explanation: because it is two objects, it is equivalent to two big houses, which do not interfere with each other and have two keys. Locks target objects, not methods, so they can be executed concurrently without mutual exclusion. Figuratively speaking, because each thread calls a method with a new object, there will be two spaces and two keys.

Synchronized modified static method

Synchronized modifies a static method. In fact, it locks this kind of object, commonly known as "class lock".

Situation 1: call two different synchronization methods directly in two threads by class.

Result: mutual exclusion occurs.

Explanation: locking a static object actually locks a class (. Class). There is only one class object. It can be understood that there is only one space at any time. There are n rooms and a lock. Therefore, rooms (synchronization methods) must be mutually exclusive.

Note: the above situation is the same as that of declaring an object in singleton mode to call non static methods, because there is always only one object. Therefore, access synchronization methods must be mutually exclusive.

Situation 2: call static or non static methods in two threads with a static object of a class.

Result: mutual exclusion occurs.

Explanation: because it is an object call, the same as above. When static methods are called, the same class lock uses the same class object.

When non static methods are called, it is equivalent to the same object lock.

Case 3: an object calls a static synchronization method and a non static synchronization method in two threads respectively

Result: mutual exclusion is not generated.

Explanation: although it is an object call, the lock types of the two methods are different. The static methods called are actually called by class objects, that is, the two methods do not produce the same object lock, so they will not be mutually exclusive and will be executed concurrently.

example

If two instances of something class A and B are added, which of the following methods can be accessed by more than one thread at the same time?

Here, we can clearly judge:

a. All are synchronized domain accesses to the same instance, so they cannot be accessed at the same time

b. It is for different instances, so it can be accessed at the same time

c. Because it is static synchronized, there is no restriction between different instances

d. The answer in the book can be accessed at the same time. The reason for the answer is that the instance method of synchronized is different from the class method of synchronized because of lock.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>