First, the origin of the problem
The origin of this article is actually bloggers want to learn java concurrency , the pain point of computer concurrency is that on the one hand CPU more and more nuclear, in addition
One aspect is that the speed gap between CPU and storage and communication subsystem is too large, presumably, the CPU speed is NS, memory 100ns, HDD MS, memory is 100 times times slower than CPU,
Hard disk 1 million times times slower than the CPU, CPU if-and so many of these slow small partners, will inevitably cause the waste of computing resources. Physical machines in order to solve the CPU and memory speed difference,
A cache is added between the two, and a new problem is introduced in the cache, which is the cache consistency issue.
Figure 1 (Image from Reference "1")
In order to solve the cache consistency problem, each CPU needs to follow a certain cache consistency protocol when accessing the cache and main memory.
Abstracting the access to a particular memory and cache is called a " memory model ".
Second, the JVM's memory model
As shown, similar to the structure of CPU access memory in a physical machine, all variables in the JVM memory model are stored in main memory
Each thread has its own working memory (working memories), and the transfer of variables between different threads needs to pass through main memory.
Figure 2 (Image from Reference "1")
As mentioned above, the transfer of variables between threads requires the interaction between working memory and main memory. This interaction follows a certain consistency protocol.
This is simply referred to as the "Interaction Protocol", which is embodied in 8 atomic operations and some operating rules, as follows:
>8 Zhongyuan Sub-operation
- Lock: A variable that acts on the main memory and identifies a variable as a thread-exclusive state;
- Unlock: A variable acting on the main memory, releasing a variable from the exclusive state of a thread, and releasing the variable before it can be locked by another thread;
- READ: A variable that acts on the main memory, transferring a variable from main memory to the working memory of the thread, and this step is only the transfer action;
- Load: A variable that acts on the working memory and puts the variable transferred in read into a copy of the working memory;
- Use: A variable acting on the working memory, passing a variable in the working memory to the execution engine, that is, the thread is reading the value of the variable;
- Assign: A variable that acts on a working memory, assigns a value that is received from the execution engine to the working memory, that is, the value of the thread in the write variable;
- Store: A variable that acts on a working memory, transferring the value of a variable to the main memory, which is simply a transfer action;
- Write: A variable that acts on the main memory, placing the value of the store's variable in the main memory variable.
> Operating Rules
Not to be continued ...
Reference documents:
"1" in-depth understanding of Java Virtual Machine by Zhou Zhiming
"2" Java concurrent programming actual combat Machinery Industry Press 2012
The memory model of the Java Virtual machine