当前位置:网站首页>JMM memory model concept
JMM memory model concept
2022-07-29 06:48:00 【The evil way does not miss the firewood cutting skill】
JMM Memory model concept
We know CPU Direct operation of cache registers , Instead of directly manipulating the main memory , If there is cache , It will cause data consistency problems , So in order to solve these problems , Put forward JMM standard .
JMM Its full name is Java Memory Model,Java Virtual machine defines a logical specification or abstract concept , Used to shield access differences between various hardware and operating system memory , Let's realize Java The program can achieve consistent memory access effect under various platforms . In other words, this set of specifications defines that the reading and writing of shared variables in the program can be mutually visible , Especially in the case of multithreading read and write operations , All around the atomicity of multithreading 、 visibility 、 Orderly unfolded .
Yes JMM You should also know something , as follows :
no need Java Memory area division of virtual machine , Is the existence of two kinds of logic , It is the result of the division of different levels , There are different emphases .
The main memory can correspond to Java Heap memory in , Working memory can correspond to part of the stack .
At a lower level , The main memory corresponds to the physical memory of the hardware , Working memory corresponds to registers and cache registers .
JMM Schematic diagram of operation memory

From the figure, we can find that the thread operates on shared variables , You need to copy a copy of the shared variables in the main memory to your own working memory , Then you can operate , After the operation, it is written back to the main memory . If considering the high concurrency scenario , Each thread first modifies the value in its own working memory , Suppose a thread assigns the value back first , At this time, the value of the main memory has been modified , But another thread is still operating with the data in its working memory , Then I wrote it back , Obviously, there is a problem with the data written back by the second thread , So how can we guarantee , Let a thread modify the value in main memory , Then notify other threads , This involves the visibility of threads , It's also JMM One of the three characteristics , Before we understand thread visibility, let's take a look JMM It's for me 8 Atomic operation to assist the interaction between threads and memory ?
JMM 8 Atomic operation ( Memory to memory interaction )
These eight atomic operation classes are also the basic steps to realize thread visibility , as follows :
lock: lock , Identify variables as thread exclusive , Operates on the main memory variableunlock: Unlock , Release locked variables , Only other threads can use , For main memoryread: Read , Read the variable value from the main memory to the working memory ( Just read the working memory , Not assigned to a variable )load: load , hold read The read variables are put into the working memory Copy variableuse: Use , Pass the value of a variable in the working memory to the execution engineassign: assignment , Assign the value received from the execution engine to the copy variable in the working memorystore: Storage , Transfer variables in working memory to main memory ( Just stored in main memory , Not assigned to a variable )write: write in , hold store The stored value is written into the main memory Shared variables
The following figure shows that two threads simultaneously control shared variables in main memory flag Modification process :
First, the thread should read the value of the shared variable from the main memory , First, lock the shared variable (lock operation ), And then through read operation , hold flag The value of is read from main memory into working memory ( It's actually a cache register ), And then through load operation , Put the working memory flag The value of is loaded into the variable copy , And then through use operation , Pass the value of the variable to CPU Execution engine .
CPU After execution , adopt assign operation , Store the calculation results in the variable copy , Then passed store operation , Copy the variable's flag Values are passed from working memory to main memory , And then execute write operation , hold flag The value rewritten of will be written to the shared variable in the main memory flag, Finally through unlock The operation thread releases the lock , Other threads can lock this variable , Repeat the above process .
Then there are several rules of memory interaction that need attention :
- Don't allow read and load、store and write The operation appears separately , The above two operations must be performed in sequence , But continuity is not guaranteed , in other words read and load、store and write Other instructions can be inserted between
- A thread is not allowed to discard its latest assign operation , That is, after the variable is changed in the working memory, the change must be synchronized to the main memory
- A thread without reason is not allowed ( There is nothing assign Modify the assignment operation ) Synchronizing data from the working memory will affect the main memory
- A new variable can only be born from main memory , It is not allowed to use an uninitialized variable directly in working memory , That is to say, to implement... On a variable user and store Before the operation , Must be executed first load and assign operation
- Only one thread is allowed to control a variable at the same time lock operation , but lock The operation can be repeated many times by the same thread , many times lock after , Only execute the same number of times unlock operation , Variables will unlock
- If you execute on a variable lock operation , Will empty the value of this variable in working memory , Before the execution engine uses this variable , Need to re load
- If a variable is not lock lock , It is not allowed to be executed unlock operation , Also can not unlock A variable that is locked by another thread
- Execute on a variable unlock Before , This variable must be synchronized to the main memory first ( perform store and write operation )
边栏推荐
猜你喜欢

day03_1_流程控制

Hongke shares | testing and verifying complex FPGA design (2) -- how to perform global oriented simulation in IP core

OpenResty的核心与cosocket

Tcp/ip 五层参考模型以及对应的典型设备以及ipv6

SQL developer graphical window to create database (tablespace and user)

day06_ Classes and objects

Joint modeling of price preference and interest preference in conversation recommendation - extensive reading of papers

三、广域通信网

NLP word segmentation

注解(Annotation)
随机推荐
LVM逻辑卷组的管理
Hongke automation SoftPLC | modk operation environment and construction steps (1) -- Introduction to operation environment
day02_基本语法
Best example of amortized cost
SDN拓扑发现原理
Phishing mail disposal
Callable 的使用
Annotation
Computer right mouse click always turn around what's going on
C语言内存-栈与堆使用
Six common ways for hackers to attack servers
Solve the error that the simulation output is STX under the frequency division module Modelsim
Thinkphp5 frequently asked questions
Hongke will share the EtherCAT demo for you and teach you how to quickly transition from other protocols to EtherCAT industrial bus
Hongke share | let you have a comprehensive understanding of "can bus error" (III) -- can node status and error counter
Instant 新日期类的使用 API
Explain the difference between FIR filter and IIR filter in detail
Hongke automation SoftPLC | Hongke kPa modk operation environment and construction steps (3) -- modk routine test
Hongke case | PAC: an integrated control solution integrating SoftPLC control logic, HMI and other service functions
非常实用的 Shell 和 shellcheck