Three ways to solve (guarantee) thread safety issues

Thread synchronization

When we use multiple threads to access the same resource, and multiple threads have write operations on the resource, thread safety issues are prone to appear.
To solve the above-mentioned multi-threaded concurrent access to a resource security problem: that is, to solve the problem of duplicate tickets and non-existent tickets, Java provides a synchronization mechanism
(synchronized) to solve it.
According to the brief description of the case: when the
window 1 thread enters the operation, the window 2 and window 3 threads can only wait outside, and the window 1 operation ends, and the window 1, window 2 and window 3 have the opportunity to enter the code
to execute. That is to say, when a thread modifies a shared resource, other threads cannot modify the resource. After the modification is completed and synchronized, it can grab CPU
resources and complete the corresponding operation, which ensures the synchronization of data and solves the problem of thread insecurity. phenomenon.
In order to ensure that each thread can perform atomic operations normally, Java has introduced a thread synchronization mechanism.
So how to use it? There are three ways to complete the synchronization operation:
1. Synchronize the code block.
2. Synchronization method.
3. Lock mechanism.
Thread safety issues appeared in
the ticket sales case. Non-existent tickets and duplicate tickets were sold

 

A solution to the problem of thread safety: use synchronized code blocks


Format:
synchronized (lock object) {_
code that may have thread safety issues (code that accesses shared data)
}
Note:
1. Through the lock object in the code block, any object can be used
2. However, multiple objects must be guaranteed The lock object used by the thread is the same
3. Lock object function: lock
the synchronization code block, and only let--thread execute in the synchronization code block

Thread synchronization technology principle

Solving thread safety issues: synchronization methods

Synchronization method: The method modified with synchronized is called the synchronization method, which ensures that when thread A executes the method, other threads can only
wait outside the method .
Format:
public synchronized void method(){}
code that may cause thread safety issues
Who is the synchronization lock?
For non-static methods, the synchronization lock is this.
For the static method, we use the bytecode object (class name.class) of the class where the current method is located.

Static synchronization method
Who is the lock object? It
cannot be this
This is generated after the object is created, and the static method takes precedence over the object
. The lock object of the static method is the class attribute of this class-->closs file object (reflection)

The third way to solve thread-safe synchronization: Lock

The java.util. concurrent. locks. Lock mechanism provides a wider range of locking operations than the synchronized code block and synchronized method. The
synchronized code block/synchronized method has the functions L.ock, in addition to being more powerful and more oriented Object.
Lock lock is also called synchronous lock. The methods of locking and releasing the lock are changed as follows:
● public void lock(): Add synchronous lock.
●public void unlock(): Release the synchronization lock.

Guess you like

Origin blog.csdn.net/weixin_51980491/article/details/113030502