当前位置:网站首页>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 )
边栏推荐
- STP spanning tree principle and example of election rules
- Is it OK to directly compare the size of two numbers in FPGA?
- 损失函数——交叉熵损失函数
- Multithreaded server programming
- Hongke shares | how to test and verify complex FPGA designs (1) -- entity or block oriented simulation
- Callable 的使用
- 5G服务化接口和参考点
- Traffic characteristics of webshell management tools
- 10种常见的软件架构模式
- How to pre circumvent the vulnerabilities of unsafe third-party components?
猜你喜欢
Online multiplayer chat room based on UDP communication
Hongke shares | testing and verifying complex FPGA design (2) -- how to perform global oriented simulation in IP core
将源码包转换为rpm包
4、 LAN and man
软件定义边界SDP
如何画出优秀的架构图
成长为架构师途中的一些思考
Embedding理解+代码
Relationship between subnet number, host number and subnet mask
Ansible(自动化软件)
随机推荐
Instant 新日期类的使用 API
Tcp/ip 五层参考模型以及对应的典型设备以及ipv6
'function vtable for error: undefined reference to ... ' 问题的原因及解决方法
day03_ 1_ Process control
Handwritten digit recognition using neural network
vmstat 内存消耗查询
IGMP protocol software development experiment
PhantomReference 虚引用代码演示
Complex floating point multiplication of vivado IP core floating point
Design and simulation code of 4-bit subtracter based on FPGA
DDoS details
SQL developer graphical window to create database (tablespace and user)
TCP based online dictionary
多线程并发下的指令重排问题
网站被挂马的解决方案
JMM 内存模型概念
20个hacker神器
Right value reference and mobile construction
Recurrent neural network RNN
NLP-分词