The difference between synchronized and static synchronized
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. Basic features 1. It starts with an optimistic lock, and if lock conflicts are frequent, it is converted to a pessimistic lock. 2. It starts with a lightweight lock implementation, and if the lock is held for a long time, it is converted to a heavyweight lock. 3. The spin lock strategy that is most likely used when implementing lightweight locks 4. It is an unfair lock 5. It is a reentrant lock 6. It is not a read-write lock 2. The JVM will synchronize the locking process Locks are divided into no lock, biased lock, lightweight lock, and heavyweight lock states. It will be upgraded sequentially according to the situation. Biased lock assumes that the male protagonist is a lock and the female protagonist is a thread. If only this thread uses this lock, then the male protagonist and the female protagonist can live happily forever even if they do not get a marriage certificate (avoiding high-cost operations). But the female supporting role appears

1. The concept of lock in Java Spin lock: When a thread acquires a lock, if the lock has been acquired by another thread, then the thread will wait in a loop, and then continue to judge whether the lock can be successfully acquired until it is acquired. The lock will exit the loop. Optimistic locking: Assuming there is no conflict, if the data is found to be inconsistent with the previously acquired data when modifying the data, the latest data will be read and the modification will be retried. Pessimistic lock: Assume that concurrency conflicts will occur, synchronize all data-related operations, and start locking from the time the data is read. Exclusive lock (write): Add a write lock to the resource. The thread can modify the resource, but other threads cannot lock it again (single write). Shared lock (read): After adding a read lock to a resource, it can only be read but not modified. Other threads can only add read locks and cannot add write locks (multiple). See as S

Summary of how to use synchronized in Java 1. When synchronized is used as a function modifier, the sample code is as follows: Publicsynchronizedvoidmethod(){//….} This is the synchronization method. So which object is synchronized locked at this time? What he locks is calling this synchronized method object. In other words, when an object P1 executes this synchronization method in different threads, they will form mutual exclusion to achieve synchronization effect. However, another object P2 generated by the Class to which this object belongs can arbitrarily call this method with the synchronized keyword added. The sample code above, etc.

1. Explain that synchronized is our most commonly used synchronization method, and there are three main ways to use it. 2. Example//General class method synchronization synchronizedpublidvoidinvoke(){}//Class static method synchronization synchronizedpublicstaticvoidinvoke(){}//Code block synchronization synchronized(object){}The difference between these three methods is that the synchronized objects are different. Ordinary classes synchronize the object itself, static methods synchronize the Class itself, and code blocks synchronize the objects we fill in the brackets. What collections are there in Java?

Tool preparation Before we formally talk about the principle of synchronized, let's talk about spin locks first, because spin locks play a big role in the optimization of synchronized. To understand spin locks, we first need to understand what atomicity is. The so-called atomicity simply means that each operation is either not done or done. Doing all means that it cannot be interrupted during the operation. For example, to add one to the variable data, there are three steps: Load from memory into register. Add one to the value of data. Write the result back to memory. Atomicity means that when a thread is performing an increment operation, it cannot be interrupted by other threads. Only when this thread completes these three processes

What is Synchronized? Java readers are no strangers to the synchronized keyword. It can be seen in various middleware source codes or JDK source codes. For readers who are not familiar with synchronized, they only know that the synchronized keyword needs to be used in multi-threading. synchronized can ensure thread safety. It is called: mutex lock (only one thread can execute at the same time, other threads will wait), also called: pessimistic lock (only one thread can execute at the same time, other threads will wait). The JVM virtual machine will help you implement it. , developers only need to use the synchronized keyword. When using it, you need to use an object as a mutex for the lock

Summary: The synchronized keyword is provided in Java to ensure that only one thread can access the synchronized code block. Since the synchronized keyword has been provided, why is the Lock interface also provided in the Java SDK package? Is this unnecessary reinvention of the wheel? Today, we will discuss this issue together. The synchronized keyword is provided in Java to ensure that only one thread can access the synchronized code block. Since the synchronized keyword has been provided, why is the Lock interface also provided in the Java SDK package? Is this unnecessary reinvention of the wheel? Today, let’s discuss it together

Java provides some other modifiers to provide functionality beyond visibility. These modifiers are called non-access modifiers. Static Members declared as static are common to all instances of the class. Static members are class-level members that are stored in class memory. Final This modifier is used to restrict further modifications to a variable, method, or class. The value of a variable declared final cannot be modified once it obtains its value. The Final method cannot be overridden in a subclass, nor can a subclass of the Final class be created. Abstract This modifier can be used with a class or method. You cannot apply this modifier to variables and constructors. Methods declared abstract must be modified in subclasses. You cannot instantiate a class declared abstract. Synchronous This modifier is used to control multiple threads
