当前位置:网站首页>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 .
边栏推荐
- Top 10 Free 3D modeling software for beginners in 2022
- 地宫取宝(记忆化深搜)
- Golang panic recover custom exception handling
- kotlin位运算的坑(bytes[i] and 0xff 报错)
- Oracle sequence + trigger
- SystemVerilog learning-08-random constraints and thread control
- UOW of dev XPO comparison
- ArcServer密码重置(账号不可以重置)
- Tidb database characteristics summary
- OpenGL es: (1) origin of OpenGL es (transfer)
猜你喜欢

69 cesium code datasource loading geojson

3D printer threading: five simple solutions

Make Tiantou village sweet. Is Xianjing taro or cabbage the characteristic agricultural product of Tiantou Village

three.js小结

HCM Beginner (I) - Introduction

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

Smartinstantiationawarebeanpostprocessor of the extension point series determines which construction method to execute - Chapter 432

无限水平大理石游戏

2022 年面向初学者的 10 大免费 3D 建模软件
![[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
随机推荐
OpenGL es: (5) basic concepts of OpenGL, the process of OpenGL es generating pictures on the screen, and OpenGL pipeline
[file system] how to run squashfs on UBI
HDU - 1501 zipper (memory deep search)
ABP 学习解决方案中的项目以及依赖关系
Using Baidu map to query national subway lines
[summary of problem thinking] Why is the register reset performed in user mode?
ONEFLOW source code parsing: automatic inference of operator signature
栈题目:解析布尔表达式
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
3D printer threading: five simple solutions
图片服务器项目测试
Uniapp tree level selector
记磁盘扇区损坏导致的Mysql故障排查
Linux closes the redis process SYSTEMd+
Record currency in MySQL
数据库产生死锁了请问一下有没有解决办法
阶乘约数(唯一分解定理)
68 Cesium代码datasource加载czml
1034 Head of a Gang
让田头村变甜头村的特色农产品是仙景芋还是白菜