The Java synchronized keyword marks a block of code or a method as a synchronized block. A synchronized code block is code that can only be executed by one thread at a time, and the thread executing the code holds a synchronization lock. The synchronized keyword can operate on
- A code block
- A method for the
When a method or block of code is declared synchronized, if one thread is executing the synchronized method or block, other threads are blocked until the thread holding the synchronized lock is released. According to the scope of lock can be divided into
- Class-level locking prevents multiple threads from simultaneously entering the synchronized code block of all instantiated objects of that class at runtime.
- Object-level locking prevents multiple threads from simultaneously entering the synchronized block of the currently instantiated object (or one of them) at runtime.
1. Object-level synchronization lock
Object level synchronization lock: When we want to execute a non-static method or code block synchronously in a multi-threaded environment, adding the synchronized keyword to a class’s methods or code block ensures thread-safe object instance level data. (Compare class-level synchronization locks later, come back to understand this sentence.)
The code for object-level locking is as follows, for example, locking a method on an instantiated object of the current class
public class DemoClass{
public synchronized void demoMethod(){}
}
Copy the code
For example, to lock a code block, the lock object is this
Public class DemoClass{public void demoMethod(){synchronized (this){synchronized (this)}}Copy the code
For example, to lock a code block, the lock object is any object we create. Do not use non-final member variables as synchronized lock objects, because non-final member variables can be reassigned, causing different threads to use different objects as locks, which cannot achieve the effect of synchronous locking.
Public class DemoClass{private final Object lock = new Object(); private final Object lock = new Object(); Public void demoMethod(){synchronized (lock){// synchronized (lock)}}Copy the code
2. Synchronization lock of class level
A class-level lock prevents multiple threads from entering the “synchronized block “of all instantiated objects of that class at runtime. That is, if there are 100 instances of DemoClass at run time, only one thread at a time can execute demoMethod() in any instance, and all other threads in all other instances are locked.
To keep static data thread safe, class-level locking should be used. We know that the static keyword associates method data to the class level, so locks are used on static methods.
A static method locks all instantiated objects of that class
Public synchronized static void demoMethod(){}} Public synchronized static void demoMethod(){}Copy the code
Gets a reference to the.class class, class level lock
Public class DemoClass{public void demoMethod(){// Get a reference to the.class class, class level lock, Synchronized (democlass.class){// synchronized code block}}Copy the code
Use static object locks, class level locks
Private final Static Object lock = new Object(); private final static Object lock = new Object(); Public void demoMethod(){synchronized (lock){public void demoMethod(){synchronized (lock){Copy the code
3. Summary
- The synchronization mechanism in Java ensures that two or more threads cannot simultaneously execute a method that requires the same synchronization lock.
- The “synchronized “keyword can only be used for methods and code blocks. These methods or code blocks can bestaticorThe static.
- When a thread enters
synchronized
Method or block of code, it acquires a lock, which it releases when it leaves the synchronized method or block. The lock is also released if any errors or exceptions occur during thread execution. - Locks held using the “synchronized “keyword are reentrant in nature, meaning that if a synchronized method calls another synchronized method using the same lock, the current thread holding the lock can enter the method without acquiring the lock again.
- Java Synchronized throws a NullPointerException if the object used in the synchronized block is empty
- use
synchronized
The synchronous approach imposes performance costs on your application. Therefore, use synchronization only when absolutely necessary. It is also a priority to use synchronized code blocks and synchronize only critical parts of the code. - Statically synchronized and non-statically synchronized methods can run simultaneously or concurrently because they use different locks.
- According to the Java language specification, you cannot use it in constructors
synchronized
The keyword. This is illegal and causes a compilation error. - Do not use non-final member variables as synchronized lock objects, because non-final member variables can be reassigned, causing different threads to use different objects as locks, which cannot achieve the effect of synchronous locking.
- Do not use string literals as lock objects, such as:
String a = "1";
Because they can be referenced elsewhere in the application and can cause deadlocks. withnew
The string object created by the keyword is safe to use.
Welcome to my blog, where there are many fine collections
- This article is reprinted with a credit (must be accompanied by a link, not only the text) : Antetokounmpo blog.
Feel helpful to you, help me like, share! Your support is my inexhaustible creative power! . In addition, the author recently a period of time output as follows boutique content, looking forward to your attention.
- Spring Boot2.0 by Hand
- Spring Security- JWT-OAUTH2
- RBAC Authority Management System for Actual Combat Front-end and Back-end Separation
- “Actual SpringCloud Micro-service from Bronze to King”
- VUE Series