当前位置:网站首页>JMM details
JMM details
2022-07-01 06:20:00 【qq_ fifty-two million twenty-five thousand two hundred and eight】
1. Talk about what is JMM-java Memory model
JMM Namely Java Memory model (java memory model). Because under different hardware manufacturers and different operating systems , There are some differences in memory access , Therefore, the same code will run on different systems and there will be various problems . therefore java Memory model (JMM) Mask the memory access differences of various hardware and operating systems , In order to make java The program can achieve the same concurrent effect under various platforms .
Java Memory model rules All variables are stored in main memory in , Include instance variables , Static variables , But excluding local variables and method parameters . Each thread has its own working memory , The working memory of a thread holds a copy of the variables and main memory used by the thread , Thread operations on variables are carried out in working memory . Threads cannot directly read or write variables in main memory .
Different threads cannot access variables in each other's working memory . The transfer of variable values between threads needs to be completed through main memory .

The working memory of each thread is independent , Thread operation data can only be performed in working memory , Then swipe back to main memory . This is a Java The basic working mode of threads defined by the memory model .
2. that JMM What is defined ?( The three major characteristics )
(1) Atomicity
Atomicity means that an operation is indivisible , uninterruptible , A thread will not be interfered by other threads during execution .
int i = 2;
int j = i;
i++;
i = i + 1;
The first sentence is basic type assignment operation , It must be atomic operation .
Read the second sentence first i Value , And then assign it to j, Two step operation , Atomicity is not guaranteed .
The third and fourth sentences are actually equivalent , Read first i Value , Again +1, Finally, assign a value to i, Three steps , Atomicity is not guaranteed .
JMM Only basic atomicity is guaranteed , If you want to guarantee the atomicity of a code block , Provides monitorenter and moniterexit Two bytecode instructions , That is to say synchronized keyword . So in synchronized Operations between blocks are atomic .
Solution :synchronized,lock, Atomic classes
(2) visibility
Visibility is when a thread changes the value of a shared variable , Other threads can immediately know that they have been modified .Java It's using volatile Keyword to provide visibility .
When the variable is volatile When decorating , This variable will be refreshed to main memory immediately after being modified . When other threads need to read the variable , Will go to the main memory to read the new value .
Solution : except volatile Besides keywords ,final and synchronized Visibility can also be achieved .
synchronized The principle is , The execution of the , Get into unlock Before , Shared variables must be synchronized to main memory .
final Decorated field , Once the initialization is complete , If no objects escape ( Object can be used by other threads for initialization ), So it's visible to other threads .
(3) Orderliness
Solution :synchronized,volatile
stay Java in , have access to synchronized perhaps volatile Ensure the order of operation between multiple threads . There are some differences in the implementation principles :
volatile The key is to use memory barrier to prevent instruction reordering , To ensure order .
synchronized The principle is , One thread lock after , must unlock after , Other threads can restart lock, To cause to be synchronized Wrapped code blocks are executed serially between multiple threads .
3. Let's talk about eight memory interactions

- lock( lock ), Variables acting on main memory , Identify variables as thread exclusive state .
- read( Read ), A variable acting on main memory , Transfer the value of the variable from main memory to the working memory of the thread , So that the next step load Operation and use .
- load( load ), A variable acting on working memory , hold read Put the variables in main memory into the variable copy of working memory .
- use( Use ), A variable acting on working memory , Transfer variables from working memory to the execution engine
- assign( assignment ), A variable acting on working memory , It assigns a value received from the execution engine to a variable copy in working memory ,
- store( Storage ), A variable acting on working memory , It transfers the value of a variable from working memory to main memory , So that the follow-up write Use .
- write( write in ): Variables acting on main memory , It is the store The value of the variable obtained by the operation from working memory is put into the variable in main memory .
- unlock( Unlock ): A variable acting on main memory , It releases a locked variable , Freed variables can be locked by other threads .
I'll add that JMM Yes 8 A rule for memory interaction :
- Don't allow read、load、store、write One of the operations occurs separately , That is to say read After operation, you must load,store After operation, you must write.
- Thread is not allowed to discard its latest assign operation , That is, after the variable data in working memory is changed , Must tell main memory .
- Threads not allowed will not have assign From working memory to main memory .
- A new variable must be born in main memory , Working memory is not allowed to use an uninitialized variable directly . It's about implementing use、store Before the operation , Must go through load and assign operation .
- A variable can only be processed by one thread at a time lock operation . many times lock after , Must be performed the same number of times unlock To unlock .
- If you do... For a variable lock operation , The value of this variable in all working memory is cleared . Before the execution engine uses this variable , Must be renewed load or assign The operation initializes the value of the variable .
- If a variable is not lock, It can't be unlock operation . Also can not unlock A variable locked by another thread .
- A thread performs... On a variable unlock Before the operation , You must first synchronize this variable back to main memory .
边栏推荐
猜你喜欢

【ManageEngine卓豪】网络运维管理是什么,网络运维平台有什么用

PLA not pasted on the bed: 6 simple solutions

Understanding of C manualresetevent class
![[summary of knowledge points] chi square distribution, t distribution, F distribution](/img/a6/bb5cabbfffb0edc9449c4c251354ae.png)
[summary of knowledge points] chi square distribution, t distribution, F distribution

手把手教你实现一个深度学习框架...

【企业数据安全】升级备份策略 保障企业数据安全

让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村

【ManageEngine卓豪 】助力世界顶尖音乐学院--茱莉亚学院,提升终端安全

OpenGL es: (3) EGL, basic steps of EGL drawing, eglsurface, anativewindow

蚂蚁新村田头村变甜头村 让厦门灌口镇田头村变甜头村的特色农产品之一是
随机推荐
交换机配置软件具有的作用
Freeswitch dial the extension number
Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village
golang panic recover自定义异常处理
Kubedm builds kubenetes cluster (Personal Learning version)
HCM Beginner (III) - quickly enter pa70 and pa71 to browse employee information PA10
【ManageEngine卓豪】助力黄石爱康医院实现智能批量化网络设备配置管理
Oracle sequence + trigger
高阶-二叉搜索树详解
Fixed height of the first column in El table dynamic header rendering
[file system] how to run squashfs on UBI
【文件系统】如何在ubi之上运行squashfs
Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
SystemVerilog learning-07-class inheritance and package use
【KV260】利用XADC生成芯片温度曲线图
Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village
Pla ne colle pas sur le lit: 6 solutions simples
DHT11 温湿度传感器
Factorial divisor (unique decomposition theorem)
Treasure taking from underground palace (memory based deep search)