synchronized usage summary
The impact of synchronized on the code when used in different places:
1. synchronized keyword modification method
Assume that P1 and P2 are different objects of the same class. This class defines synchronized blocks or synchronized methods in the following situations, P1 , P2 can call them.
public synchronized void method(){
//
}
This is the synchronized method, and what synchronized locks at this time is the call to 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. At the same time, if there are multiple synchronized methods in the object, when a thread executes a synchronized method in the object, other synchronized methods in the object will not be allowed to be executed by other threads. 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 is equivalent to the following code:
public void method() {
synchronized (this)
{
Only the thread of P1 can call the synchronization method of P1. For P2, the lock of P1 has nothing to do with it. The program may also escape the control of the synchronization mechanism in this case, causing data confusion.
2. Synchronized block, the sample code is as follows:
public void method(SomeObject so) {
synchronized(so)
{ //..
}
}
At this time, the lock is the so object, and each object corresponds to a unique lock , so whichever thread gets the object lock can run the code he controls. When there is a clear object as the lock, you can write the program like this, but when there is no clear object as the lock and you just want to synchronize a piece of code, you can create a special instance variable (it must be an object) to act as the lock:
private byte[] lock = new byte[0];
Public void method(){
synchronized(lock) }
}
3. Apply synchronized to static functions. The sample code is as follows:
Class Foo
{ public synchronized static void method1()
{ //.
}
public void method2()
{
synchronized(Foo.class) /
}
}
It can be inferred: If a synchronized static function A is defined in a class, and a synchronized instance function B is also defined, then when the same object Obj of this class accesses the A and B methods respectively in multiple threads, it will not It constitutes synchronization because their locks are all different. The lock of method A is the Class to which Obj belongs, and the lock of method B is the object to which Obj belongs.

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

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?

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.

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

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

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

1. The synchronized implementation of the lock modifies the instance method. For the ordinary synchronization method, the lock is the current instance object modifying the static method. For the static synchronization method, the lock is the current Class object modification method code block. For the synchronization method block, the lock is The objects configured in synchronized brackets! When a thread attempts to access a synchronized code block, it must obtain the lock, and when it is completed (or an exception occurs), it must release the lock. So where exactly does the lock exist? Let’s explore together! However, I believe that since you can find this article, I believe that you are already familiar with its use. We will not give a detailed explanation of its use and why the data will be confused in multi-threaded situations.
