This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”
What is the JMM?
JMM refers to the Java Memory Model, also known as the Java Memory Model
- The Java memory model is not an actual thing, but an artificial convention, a concept.
There are a few synchronization conventions we need to know about the JMM:
- Before unlocking, the thread must immediately flush the values stored in the thread’s working memory to the shared variables in main memory!
- Before locking, the thread must read the latest value in main memory into working memory!
- The lock and the unlock are the same lock!
Data operated by a thread is read from main memory and backed up to the thread’s own working memory. As a copy, main memory does not actively update data to the thread.
Eight memory interoperations of threads:
- Lock: A variable that acts on main memory, identifying a variable as thread-exclusive
** * Unlock ** : Variable that acts on main memory, freeing a shared variable that has been locked ** * Read ** : Variable that acts on main memory, transferring the value of a variable from main memory to the thread's working memory ** *load ** : A variable that acts on working memory, putting the value of the variable obtained by the read operation into the working memory ** * ** * Assign ** : Assign the value of the variable assigned to the working memory to the working memory ** *store ** : Store the value of the variable assigned to the working memory to the execution engine ** : A variable that acts on main memory to transfer the value of a variable from working memory in a thread to main memory for subsequent write operations ** *write ** : a variable that acts on main memory to store the value of a variable from working memory into main memoryCopy the code
The JMM imposes the following constraints on the preceding eight memory operation instructions:
-
read
andload
,user
andassign
,store
andwrite
,lock
andunlock
Must come in pairs, and no one instruction is allowed to operate on it alone- A thread is not allowed to discard the nearest thread
assign
Operation, that is, when the value of a variable in working memory changes, main memory must be notified - Not allowed a thread will not have
assign
Data over synchronization from working memory will be synchronized to main memory - A new variable must be created in main memory, and working memory is not allowed to privately initialize a variable as a shared variable, i.e., implementation
use
andstore
Before operation, must pass load andassign
operation - Only one thread is allowed to lock a variable at a time; Many times
lock
After that, it must be executed the same number of timesunlock
To unlock - If a variable is locked, the value of the variable in all working memory will be cleared. That is, the thread that obtains the lock must re-read the value of the variable in main memory before it can submit the value to the execution engine for use
- You cannot unlock a variable that has not been locked, nor can you unlock a variable that has been locked by another thread
unlock
- Before locking a variable, the value of the variable in working memory must be synchronized back to main memory
Existing problems:
Suppose you have a main thread and a normal thread. The normal thread performs the following operation: when num is 0, it keeps looping; In this case, the main thread assigns the value of num to 1. The normal thread will not know that num has been changed, and the program will continue to execute without stopping!
public class VolatileDemo {
private static int num = 0;
public static void main(String[] args) {
new Thread(()->{ / / thread 1
while (num == 0) {
}
}).start();
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
num = 1; System.out.println(num); }}Copy the code
Solution: Volatile keyword
What is volatile?
-
Volatile is a lightweight synchronization mechanism, as opposed to synchronized
-
Ensure visibility => After the shared variable in JMM main memory is modified, all threads are notified to back up to their working memory
-
Atomicity is not guaranteed
-
Order reordering is prohibited
Finally, I wish you all an early success and satisfactionoffer, fastA promotion and pay increaseTo the top of my life.
Can please give me a three to support me??????