当前位置:网站首页>Analysis of JMM memory model
Analysis of JMM memory model
2022-07-03 11:13:00 【dot】
Java Memory model (JMM)
brief introduction
Java Memory model (Java Memory Model ,JMM) It's a memory model specification , Shielding the access differences of various hardware and operating systems , To ensure the Java Program access to memory under various platforms can ensure the mechanism and specification of consistent effect .
JVM The entity running the program is the thread , And when each thread is created JVM Will create a working memory for it ( Some places are called stack spaces ), Used to store thread private data , and Java The memory model states that all variables are stored in main memory , Main memory is a shared memory area ,
All threads have access to , But thread operations on variables ( Read assignments, etc ) Must be done in working memory , First, copy the variables from the main memory to your own working memory space , Then operate on variables , Write the variables back to main memory after the operation , You can't directly manipulate variables in main memory ,
The working memory stores a copy of the variables in the main memory , As I said before , Working memory is the private data area of each thread , So different threads can't access each other's working memory , Communication between threads ( Pass value ) It has to be done in main memory .
Why there is a memory model
Modern computers have multilevel caches
stay CPU When accessing a storage device , Whether it's accessing data or accessing instructions , All tend to gather in a continuous area , This is called the locality principle .
Temporal locality (Temporal Locality): If an information item is being accessed , So it's likely to be visited again in the near future . For example, circulation 、 recursive 、 Methods, and so on . Spatial locality (Spatial Locality): If a memory location is referenced , So in the future, the location near him will also be quoted . For example, code executed sequentially 、 Two objects created consecutively 、 Array etc.
Multicore CPU Multi level cache consistency protocol MESI
Multicore CPU In this case, there are multiple first level caches , How to ensure the consistency of the internal data in the cache , Don't let the system data mess up . This leads to a consistent protocol MESI.
Be careful : about M and E State is always accurate , They are consistent with the true state of the cache line , and S The state may be inconsistent .
Reorder
In order to improve performance when executing programs , Compilers and processors often reorder instructions .
There are three kinds of reordering :
1、 Compiler optimized reordering . Compiler without changing the semantics of single thread program , You can rearrange the execution order of statements . 2、 Instruction level parallel reordering . Modern processors use instruction level parallelism to execute multiple instructions in an overlapping way . If there is no data dependency , The processor can change the execution order of machine instructions corresponding to statements . 3、 Reordering of memory system . Because the processor uses cache and read / Write buffer , This makes loading and storage operations appear to be out of order .
JMM The main problem to be solved : When multithreads communicate through shared memory , The existing local memory data is inconsistent 、 The compiler reorders the code instructions 、 The processor will cause problems such as disorderly code execution
- The problem of cache consistency is actually the problem of visibility .
- Processor optimization can lead to atomicity problems
- The rearrangement of instructions leads to the problem of order
Eight atomic operations of data synchronization
(1)lock( lock ): A variable acting on main memory , Mark a variable as a thread exclusive state
(2)unlock( Unlock ): A variable acting on main memory , Release a variable that is locked , Freed variables can be locked by other threads
(3)read( Read ): A variable acting on main memory , Transfers a variable value from main memory to the thread's working memory , For subsequent load The action to use
(4)load( load ): A variable acting on working memory , It is the read The operation places the values of the variables obtained from main memory into a copy of the variables in working memory
(5)use( Use ): A variable acting on working memory , Passes the value of a variable in working memory to the execution engine
(6)assign( assignment ): A variable acting on working memory , It assigns a value received from the execution engine to a variable in working memory
(7)store( Storage ): A variable acting on working memory , Transfers the value of a variable in working memory to main memory , For subsequent write The operation of
(8)write( write in ): A variable acting on working memory , It is the store Operations are transferred from the value of a variable in working memory to a variable in main memory
Synchronization rule analysis
- A thread is not allowed for no reason ( It didn't happen assign operation ) Synchronize data from working memory back to main memory
- A new variable can only be created in main memory , It is not allowed to use an uninitialized one directly in working memory (load perhaps assign) The variable of . That is to say, to implement a variable use and store Before the operation , You have to do it yourself first assign and load operation .
- Only one thread is allowed to work on a variable at a time lock operation , but lock Operations can be executed multiple times by the same thread , Multiple execution lock after , Only execute the same number of times unlock operation , Variables will be unlocked .lock and unlock They have to come in pairs .
- If you execute on a variable lock operation , Will empty the value of this variable in working memory , This variable needs to be re executed before the execution engine can use it load or assign The operation initializes the value of the variable .
- If a variable is not previously lock Locking operation , It is not allowed to be executed unlock operation ; I'm not allowed to go unlock A variable that is locked by another thread .
- Execute on a variable unlock Before the operation , You must first synchronize this variable to main memory ( perform store and write operation )
Guaranteed atomicity
stay Java in , To ensure atomicity , Two advanced bytecode instructions are provided monitorenter and monitorexit. These two bytecodes , stay Java The corresponding keyword in is synchronized.
therefore , stay Java Can be used in synchronized To ensure that operations within methods and code blocks are atomic .
Guaranteed visibility
Java The memory model synchronizes new values back to main memory after variables are modified , The method of refreshing variable values from main memory before variable reading depends on main memory as the transmission medium .
Java Medium volatile Keyword provides a function , That is, the variable modified by it can be synchronized to main memory as soon as it is modified , The variable decorated by it is refreshed from main memory before each use .
Here's the conservative strategy ,volatile Write operations The instruction sequence diagram generated after inserting the memory barrier :
Here's a conservative strategy ,volatile Read operations The instruction sequence diagram generated after inserting the memory barrier :
Above volatile Write operations and volatile The memory barrier insertion strategy for read operations is very conservative . In actual execution , As long as it doesn't change volatile Write - Memory semantics of reading , The compiler can omit unnecessary barriers depending on the situation .
except volatile,Java Medium synchronized and final Two keywords can also achieve visibility .
To ensure order
stay Java in , have access to synchronized and volatile To ensure the order of operation among multiple threads . Different ways of implementation : volatile The keyword will disable command rearrangement . synchronized Keyword ensures that only one thread operation is allowed at the same time .
Speaking of order , Be careful , We say that order can be achieved by volatile and synchronized To achieve , We can't rely on both of these keywords . actually ,Java The language has provisions for reordering or ordering , These regulations cannot be violated during virtual machine optimization .
Happen-Before The principles are as follows :
1. Principle of procedure order : In one thread , In order of program code , The operation written in front comes first from the operation written in the back . 2.volatile The rules : For the same variable volatile Write operations , The read operation that occurs first and then faces this variable , This ensures that volatile Visibility of variables . 3. Locking rules : Unlock (unlock) The operation starts with the subsequent locking of the same lock (lock) operation . 4. Transitivity :A Precede B,B Precede C, that A Necessary before C. 5. Thread start rule : Thread start Method occurs first in every action of this thread . 6. Thread interrupt rule : For threads interrupt() The operation first occurs on the thread isInterrupted() operation , That is, the interrupt flag bit must be set before the interrupt can be detected . 7. Thread termination rule : All operations in the thread take precedence over the termination detection of this thread . 8. Object termination rule : Initialization of an object completed ( Constructor execution ) First occurs in the object finalize() Before method .
as-if-serial semantics
- as-if-serial Semantics means : No matter how reorder ( Compiler and processor to improve parallelism ),( Single thread ) The execution of the program does not change . compiler 、runtime And the processor must comply as-if-serial semantics .
- In order to observe as-if-serial semantics , Compilers and processors do not reorder operations that have data dependencies , Because this reordering will change the execution result . however , If there are no data dependencies between operations , These operations can be reordered by compilers and processors .
Sequential consistent memory model
1、 All operations in a thread must be performed in the order of the program .( Whether the program is synchronized or not ) All threads can only see a single sequence of operations .
2、 In the sequential consistent memory model , Each operation must be atomic and immediately visible to all threads .
Reference article :
https://cloud.tencent.com/developer/article/1462257
https://copyfuture.com/blogs-details/202206170625427409
边栏推荐
- . Net core - a queuing system for wechat official account
- CorelDRAW Graphics Suite 2022新版功能详情介绍
- 如何:配置 ClickOnce 信任提示行为
- 嵌入式软件测试怎么实现自动化测试?
- 独家分析 | 关于简历和面试的真 相
- File upload and download test point
- Snownlp emotion analysis
- 【obs】封装obs实现采集的基础流程
- Communication software development and Application
- Have you learned the new technology to improve sales in 2021?
猜你喜欢
[proteus simulation] 16 channel water lamp composed of 74hc154 four wire to 12 wire decoder
Software testing e-commerce projects that can be written into your resume, don't you come in and get it?
反正切熵(Arctangent entropy):2022.7月最新SCI论文
The testing department of the company came to the king of the Post-00 roll, and the veteran exclaimed that it was really dry, but
Qt:qss custom qpprogressbar instance
Stack, monotone stack, queue, monotone queue
The highest monthly salary of 18K has a good "mentality and choice", and success is poor "seriousness and persistence"~
17K薪资要什么水平?来看看95后测试工程师的面试全过程…
Error installing the specified version of pilot
历经一个月,终于拿到金蝶Offer!分享一下四面面经+复习资料
随机推荐
How to realize automatic testing in embedded software testing?
Google Earth Engine(GEE)——当我们前后影像来弥补插值效果得时候,没有效果怎么办?
QT: QSS custom qtabwidget and qtabbar instances
Software testing (test case) writing: vulgar, native and skillful
15 software testing Trends Worthy of attention
有赞CTO崔玉松:有赞Jarvis核心目标是使产品变得更加聪明和可靠
12. Nacos server service registration of source code analysis of Nacos service registration
(二)进制
T5 的尝试
ORACLE 11G 单机冷备数据库
The testing department of the company came to the king of the Post-00 roll, and the veteran exclaimed that it was really dry, but
What is the salary level of 17k? Let's take a look at the whole interview process of post-95 Test Engineers
Commonly used discrete random distribution
Clion debug
QT: QSS custom qtreeview instance
Software testing redis database
Qt:qss custom qgroupbox instance
Definition and properties of summation symbols
8年测试工程师总结出来的《测试核心价值》与《0基础转行软件测试超全学习指南》
Qt:qss custom qpprogressbar instance