当前位置:网站首页>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 .
边栏推荐
- 3D printer threading: five simple solutions
- freeswitch拨打分机号
- ABP 学习解决方案中的项目以及依赖关系
- 浅谈SIEM
- Although pycharm is marked with red in the run-time search path, it does not affect the execution of the program
- C# ManualResetEvent 类的理解
- OpenGL es: (4) detailed explanation of EGL API (Continued)
- ForkJoin和Stream流测试
- 指数法和Random Forest实现山东省丰水期地表水体信息
- Tidb database characteristics summary
猜你喜欢

FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview

数据库产生死锁了请问一下有没有解决办法

The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same

Excel visualization

Movable mechanical wall clock

ONEFLOW source code parsing: automatic inference of operator signature

three.js小结

Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills

Transformer le village de tiantou en un village de betteraves sucrières

让田头村变甜头村的特色农产品是仙景芋还是白菜
随机推荐
SystemVerilog learning-07-class inheritance and package use
Differences between in and exists in MySQL
π disk, turning your computer into a personal private cloud
地宫取宝(记忆化深搜)
数据库er图组成要素
Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
One of the characteristic agricultural products that make Tiantou village, Guankou Town, Xiamen into a "sweet" village is
Oracle create user + Role
端口扫描工具是什么?端口扫描工具有什么用
【ManageEngine卓豪】用统一终端管理助“欧力士集团”数字化转型
ONEFLOW source code parsing: automatic inference of operator signature
SystemVerilog learning-08-random constraints and thread control
69 cesium code datasource loading geojson
Arcserver password reset (account cannot be reset)
Diffusion (multi-source search)
SystemVerilog learning-09-interprocess synchronization, communication and virtual methods
freeswitch拨打分机号
HCM Beginner (I) - Introduction
10-golang运算符
SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack