当前位置:网站首页>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 .
边栏推荐
猜你喜欢
相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
异常检测方法梳理,看这篇就够了!
讓田頭村變甜頭村的特色農產品是仙景芋還是白菜
68 cesium code datasource loading czml
连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?
Freeswitch dial the extension number
Talking from mlperf: how to lead the next wave of AI accelerator
【文件系统】如何在ubi之上运行squashfs
Uniapp tree level selector
【ManageEngine卓豪 】助力世界顶尖音乐学院--茱莉亚学院,提升终端安全
随机推荐
【KV260】利用XADC生成芯片温度曲线图
Freeswitch dial the extension number
地宮取寶(記憶化深搜)
JDBC database operation
ONEFLOW source code parsing: automatic inference of operator signature
OpenGL es: (1) origin of OpenGL es (transfer)
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
Solve the problem of garbled files uploaded by Kirin v10
Skywalking integrated Nacos dynamic configuration
地宫取宝(记忆化深搜)
Kubedm builds kubenetes cluster (Personal Learning version)
【网络安全工具】USB控制软件有什么用
Cjc8988 Low Power Stereo codec with 2 stereo headphone drivers
Diffusion (multi-source search)
MySQL里记录货币
MySQL怎么存储emoji?
浅谈SIEM
阶乘约数(唯一分解定理)
让厦门灌口镇田头村变甜头村的特色农产品之一是蚂蚁新村
【ManageEngine卓豪】助力黄石爱康医院实现智能批量化网络设备配置管理