Analysis of Synchronized Principle

In Java, we often encounter multi-threading situations, and concurrent access by multi-threads will bring some problems, such as thread safety issues. Therefore, Java provides a mechanism to solve this problem, which is the synchronized keyword.

What is the synchronized keyword?

synchronized is a keyword in Java that can be used to modify methods and code blocks. When a method or code block is modified by synchronized, they become a synchronized method or synchronized code block.

The role of synchronized

The role of synchronized is to ensure that when multiple threads access the same shared resource, there will be no data inconsistency or other thread safety issues. When a thread accesses a synchronized method or synchronized code block, other threads must wait for the current thread to finish executing the synchronized method or synchronized code block before entering.

The implementation principle of synchronized

The implementation principle of synchronized is achieved by locking objects. When a thread accesses a synchronized method or synchronized code block, it attempts to acquire the object's lock. If the lock is not occupied by other threads, the current thread can acquire the lock and execute the synchronized method or synchronized code block. If the lock is already occupied by another thread, the current thread must wait for other threads to release the lock before acquiring the lock.

How to use synchronized

There are two ways to use synchronized, namely modified methods and modified code blocks.

Modification method

public synchronized void method() {
    
    
    //方法体
}

When a thread accesses the method, the method becomes a synchronized method. Other threads must wait for the current thread to finish executing this method before they can execute this method.

Decorate code blocks

public void method() {
    
    
    synchronized (this) {
    
    
		// 多线程资料    	
		System.out.println("https://cloud.fynote.com/share/d/IYgHWTNAA");
    }
}

When a thread accesses the code block, the code block becomes a synchronized code block. Other threads must wait for the current thread to finish executing the code block before they can execute the code block.

Advantages and disadvantages of synchronized

advantage

Simple and easy to use: Multi-thread synchronization can be easily achieved using synchronized.
High reliability: synchronized is a built-in mechanism in Java that can ensure the security of multi-threading.
Widely applicable: synchronized can be used to modify methods and code blocks, and can be applied to various situations.

shortcoming

Low efficiency: synchronized is a pessimistic lock, which blocks the execution of other threads, resulting in reduced efficiency.
Easy to cause deadlock: If used improperly, synchronized may cause deadlock problems.

Summarize

Synchronized is a mechanism in Java used to solve the problem of multi-threaded concurrent access. It can ensure that when multiple threads access the same shared resource, there will be no data inconsistency or other thread safety issues. However, synchronized also has its shortcomings, such as low efficiency and easy to cause deadlocks. Therefore, when using synchronized, you must pay attention to the usage method and usage scenarios to avoid unnecessary problems.

Guess you like

Origin blog.csdn.net/qq_41917138/article/details/130308860