当前位置:网站首页>"Ask every day" briefly talk about JMM / talk about your understanding of JMM
"Ask every day" briefly talk about JMM / talk about your understanding of JMM
2022-07-25 14:53:00 【Senior fishing Engineer】
JMM What is it?
Java Memory Model , Java Memory model , abbreviation JMM.
Java Communication between threads is made by JMM control .JMM Determines when a thread's write to a shared resource is visible to another thread . From an abstract point of view ,JMM Defines the relationship between threads and main memory , Each thread has a local memory 1, Storing shared resources in main memory 「 copy 」.
JMM Memory model

It said ,JMM Control thread communication . Thread as shown in the figure A、B How is the communication
- Threads A Refresh the shared variables in your local memory to the main memory
- Threads B Make the copy in your local memory invalid , Read the shared variable value in main memory
The above two steps can be seen
- Communication between threads must pass through main memory .
- JMM By controlling the interaction between local memory and main memory of each thread , Provide us with memory visibility assurance .
Reorder
When a program is executing , To optimize program performance , Compilers and processors often reorder instructions . Reordering can cause memory visibility problems in multithreaded programs .
JMM Treatment and requirements for these two types of rearrangement :
- For compiler reordering :JMM Will prevent certain types of compiler reordering . So what is it ?
- Reorder for processor :JMM Will ask for Java When the compiler generates a sequence of instructions , Insert a specific type of memory barrier (Memory Barries) To disable certain types of processor reordering .
JMM As a language level memory model , On different compilers and processors , By prohibiting certain types of compiler and processor reordering , Provide consistent memory visibility assurance .
Data dependency
If two operations access the same variable , And one of these two operations is a write operation , Then there is a data dependency between the two operations .
There are three operations that generate data dependencies :
- After reading
- Read after writing
- Write after
The above three operations , If the order is rearranged , Then the result of the program will be changed .
as-if-serial semantics
as-if-serial( Serialization ) The meaning of is : No matter how the compiler and processor reorder to provide parallelism , The execution result of a single threaded program cannot be changed 2.
So in order to achieve this effect , Compilers and processors do not reorder operations that have data dependencies .
Pictured ,C Respectively with A、B There are data dependencies , that C Will not be rearranged in A、B Before any one goes .
Then the last possible order is :
happens-before
If an operation happens-before Another operation , Then the execution result of the first operation is visible to the second operation , But the execution order of the first operation is not necessarily ahead of the second . If these two operations are data dependent , Then the first operation is ahead of the second , vice versa .
Multi thread operation happens-before The establishment of the relationship depends on the correct synchronization of the code .
Local memory is JMM An abstract concept of , It doesn't really exist , It actually covers caching 、 Write buffer 、 Optimization of registers and other hardware and compilers . ︎
as-if-serial Protect single threaded programs , At the same time, it will also create an illusion : The execution order of single threaded code is consistent with the order of written code . ︎
边栏推荐
- awk从入门到入土(23)awk内置变量ARGC、ARGC--命令行参数传递
- SSM framework integration, simple case
- [Nuxt 3] (十一) 传送 & 模块
- Development of uni app offline ID card identification plug-in based on paddleocr
- Software testing -- 1. Outline of software testing knowledge
- Awk from getting started to digging in (21) awk script debugging
- 27 classification of selectors
- 冈萨雷斯 数字图像处理 第一章绪论
- PHP 通过原生CURL实现非阻塞(并发)请求模式
- Alibaba cloud installs mysql5.7
猜你喜欢

51 single chip microcomputer learning notes (1)

关于ROS2安装connext RMW的进度条卡在13%问题的解决办法

河源市区推出消防安全主题奶茶 助推夏季火灾防控

PS making and loading GIF pictures tutorial

51单片机学习笔记(2)

Gameframework making games (II) making UI interface

I2C设备驱动程序的层次结构
![优质数对的数目[位运算特点+抽象能力考察+分组快速统计]](/img/c9/8f8f0934111f7ae8f8abd656d92f12.png)
优质数对的数目[位运算特点+抽象能力考察+分组快速统计]

51单片机学习笔记(1)

D2. Chopping Carrots (Hard Version) (每日一题)
随机推荐
Leetcode-198- house raiding
Development of uni app offline ID card identification plug-in based on paddleocr
Awk from getting started to digging in (23) awk built-in variables argc, argc -- command line parameter transfer
Add the jar package under lib directory to the project in idea
Vs2017 large factory ERP management system source code factory general ERP source code
Alibaba cloud installs mysql5.7
41 图片背景综合-五彩导航图
关于RDBMS和非RDBMS【数据库系统】
English语法_不定代词 - other / another
各种平台dpkg包下载地址(包括arm64)
The main function of component procurement system, digital procurement helps component enterprises develop rapidly
Examples of bio, NiO, AIO
Melodic + Realsense D435i 配置及错误问题解决
Polymorphism and interface
[C题目]力扣88. 合并两个有序数组
转载----如何阅读代码?
快速搭建Dobbo小Demo
Go语言创始人从Google离职
应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
[C题目]力扣876. 链表的中间结点