Threading is prone to thread-safety issues when working with multiple threads on the same object. To address thread safety issues, Java Multithreading introduces a synchronization monitor.
Synchronizing code blocks
The synchronization code block syntax is formatted as follows:
synchronized (obj) { // The code here is a synchronous code block }
The above syntax format, synchronized, is the synchronous monitor, and the code above means that the thread must obtain a lock on the synchronization monitor before it begins executing the synchronization code block.
Only one thread at any time can obtain a lock on the synchronization monitor, which naturally discards the lock on the synchronization monitor when the synchronization code block finishes executing.
It is generally recommended to use shared resources that may be accessed concurrently as a synchronization monitor.
Synchronization method
The method of synchronization is to use the Synchronized keyword to decorate a method, which is called a synchronous method. For synchronous methods, there is no need to explicitly specify a synchronization monitor, which is the synchronization monitor for the synchronization method, which is the current object itself.
It is important to note that the Synchronized keyword can be decorated with blocks of code, can be decorated with methods, but not decorated with constructors, properties, and so on.
In order to reduce the negative impact of thread safety, the thread safety of a mutable class is at the cost of reducing the efficiency of the program, and the following strategies can be used:
- Do not synchronize all methods of thread-safe classes, only those that change the shared resources;
- There are two versions available for mutable classes: The thread-safe version and the thread-unsafe version to accommodate different operating environments.
Release the lock on the sync monitor
When a thread enters a synchronization method or synchronizes a code block and cannot actively release the lock on the synchronization monitor, the thread releases the lock on the synchronization monitor in the following situations:
- Synchronization method, synchronization code block execution end;
- When the synchronization method is executed, the code block is synchronized, a break is encountered, return terminates the synchronous code block, and the synchronous method executes;
- In the execution of the synchronization method, the synchronization code block is encountered error or exception, an interruption occurred;
- When the synchronization method is executed, the code block is synchronized, the Wait () method of the synchronization monitor object is executed, and the synchronization monitor is paused and released by the current thread.
In the following scenario, the program does not release the lock on the synchronization monitor:
- When the synchronization method is executed and the code block is synchronized, the program calls Thread.Sleep (), Thread.yield () to pause the execution of the current thread;
- When the synchronization method is executed, and the code block is synchronized, other threads call the current thread's suspend method to suspend the thread.
Sync Lock
Java thread Synchronization