当前位置:网站首页>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 .
边栏推荐
猜你喜欢

连续四年入选Gartner魔力象限,ManageEngine卓豪是如何做到的?

ManageEngine卓豪助您符合ISO 20000标准(四)

图片服务器项目测试
![[note] e-commerce order data analysis practice](/img/03/367756437be947b5b995d5f7f55236.png)
[note] e-commerce order data analysis practice

JMM详解

Freeswitch dial the extension number

Excel visualization

One of the characteristic agricultural products that make Tiantou village, Guankou Town, Xiamen into a "sweet" village is

Arcserver password reset (account cannot be reset)

JDBC database operation
随机推荐
Flink practice -- multi stream merge
Thoughts on a "01 knapsack problem" expansion problem
HDU - 1501 zipper (memory deep search)
C# ManualResetEvent 类的理解
ONEFLOW source code parsing: automatic inference of operator signature
libpng12.so. 0: cannot open shared object file: no such file or directory
[postgraduate entrance examination advanced mathematics Wu Zhongxiang +880 version for personal use] advanced mathematics Chapter II Basic Stage mind map
srpingboot security demo
Geoffrey Hinton: my 50 years of in-depth study and Research on mental skills
XAF Bo of dev XPO comparison
让田头村变甜头村的特色农产品是仙景芋还是白菜
SystemVerilog learning-09-interprocess synchronization, communication and virtual methods
three.js小结
Fixed height of the first column in El table dynamic header rendering
Discrimination between left and right limits of derivatives and left and right derivatives
相同区域 多源栅格数据 各个像元行列号一致,即行数列数相同,像元大小相同
[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage
Kubedm builds kubenetes cluster (Personal Learning version)
可动的机械挂钟
高阶-二叉平衡树