Java Concurrency 5 - synchronized and volatile keywords

One, synchronized

Take the virtual calling system as an example

When the concurrent program is used to call the number, there will be phenomenons such as number skipping, repeated numbers, and exceeding the maximum value.

Refer to the JMM model, https://blog.csdn.net/qq_22059611/article/details/95211836

It can be seen that because the workspace data is not visible to other threads, and the applause operation is not an atomic operation, this kind of problem will occur.

In order to solve this problem, the concept of lock is introduced. When one thread operates on shared data, other threads cannot operate on the data.

1. Related concepts

Why can synchronization be achieved by using locks?

The lock mechanism has the following two characteristics:

Mutual exclusion: that is, only one thread is allowed to hold a certain object lock at the same time. Through this feature, the coordination mechanism in multi-threading is realized, so that only one thread executes on the code block (composite operation) that needs to be synchronized at the same time. access. Mutual exclusion is also often referred to as the atomicity of operations.

Visibility: It must be ensured that before the lock is released, the modification made to the shared variable is visible to another thread that subsequently acquires the lock (that is, the value of the latest shared variable should be obtained when the lock is acquired), otherwise another thread The inconsistency may be caused by continuing the operation on a locally cached copy.

2. Usage of synchronized

a. Synchronization method

(1) Synchronized non-static methods

Public synchronized void methodName(){

……

}

(2) Synchronous static method

Public synchronized static void methodName(){

……

}

b. Synchronous code block

synchronized(this|object) {}
synchronized(类.class) {}
Private final Object MUTEX =new Object();
Public void methodName(){
   Synchronized(MUTEX ){
   ……
}
}

 

 

Classification according to acquired locks

1. Get the object lock

synchronized(this|object) {}

Decorate non-static methods

In Java, each object will have a monitor object, which is actually the lock of the Java object, usually called "built-in lock" or "object lock". There can be multiple objects of a class, so each object has its own independent object lock without interfering with each other.

2. Get class lock

synchronized(类.class) {}

Decorate static methods

In Java, there is also a lock for each class, which can be called a "class lock". The class lock is actually implemented through an object lock, that is, the Class object lock of the class. There is only one Class object per class, so there is only one class lock per class.

Instructions for use

In Java, every object will have a monitor object, monitor.

1) When a certain thread owns this object, first check whether the counter of the monitor is 0, if it is 0 and no thread owns it, then the thread owns this object at this time, and adds 1 to the monitor of this object; if it is not 0, it means this The thread is already occupied by another thread, and this thread waits. When the thread releases the possession, monitor-1;

2) The same thread can lock the same object multiple times, +1, +1, reentrant

3. Analysis of synchronized principle

 

 

 

two. volatile

 

 

Guess you like

Origin blog.csdn.net/qq_22059611/article/details/103371573