In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Java memory model visibility analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1. JMM model description
Given a program and an execution trace that detects whether the program is legitimate, JMM works by checking each read in the trace and checking whether the observed writes are valid according to certain rules
The behavior that may be generated in JMM is shown as long as all the results of the program are consistent with those expected by JMM, no matter how the code implements the program behavior.
Based on the second point above, the implementation of transforming the code executed by the implementer is relatively free, and the operation can be reordered or even unnecessary synchronous operation code can be deleted.
2. Data sharing and competition of JMM
Thread sharing and exclusive area
Thread sharing area: the method area in the JVM running data area and the data variables stored in heap memory. There is data competition, that is, the security problem of data reading and writing.
Thread exclusive area: a private area created separately for each thread by JVM to store the current thread's private data variables. There is no data competition, such as thread local variables, ThreadLocal/ThreadLocalRandom, etc.
Thread communication creates data competition
Brief source code
/ / constant.java
Final int P = 10
Final int C = 20
/ / shared.java
Int pwrite = 0
Int cwrite = 0
/ / producer.java
Int pread = 0
Public void run () {
Pread = cwrite; / / the producer thread needs data from the consumer thread cwrite
Pwrite = P
}
/ / consumer.java
Int cread = 0
Public void run () {
Cread = pwrite;// consumer thread requires pwrite data of producer thread
Cwrite = C
}
/ / inferred from the expected value of the normal result output, there is no simultaneous pread = = C (20) and cread = = P (10).
Retrospective analysis of the results (based on the code order we see)
If the above execution result is true, then cwrite = C must have been executed before pread = cwrite
Since cwrite = C is executed after cread = pwrite, cread = pwrite must be executed before pread = cwrite
That is, cread = pwrite must have been executed before pwrite = P, so the result is not valid.
Create a problem
Since a thread has a write operation, must the data variable of the write operation be read by another thread to the corresponding written data?
Since the thread itself has its own working memory, the read data variable is not necessarily the data after the write operation of another thread, and the cached data (dirty data) may be read on the working memory.
At the same time, the code that may be executed after optimization is generated based on the JMM specification.
Public void run () {
Pread = cwrite; / / the producer thread needs data from the consumer thread cwrite-- 1
Pwrite = P; / /-- 2
}
/ / consumer.java
Int cread = 0
Public void run () {
Cwrite = C; / /-- 3
Cread = pwrite;// consumer thread needs pwrite data of producer thread-4
}
Analyze in the above two threads
Under concurrency, threads may produce execution in the order of 3-1-2-4, that is, pread = = C (20) and cread = = P (10) at the same time.
Data competition
The current thread writes to a variable
At the same time, another thread reads the same variable.
Read and write operations are not sorted through synchronization
Create a problem
The communication between different threads will compete for the data of shared variables. In this case, the optimization of JMM reordering will cause the output result to be inconsistent with the expected result. If placed in the actual business scenario, it will lead to a lot of uncontrollable business logic errors, and the consequences will be unimaginable.
The concurrency problem under JMM
First, the shared data read is not necessarily the data after the write operation, that is, the write operation is not visible to the read operation (caused by cache)
Second, JMM reorders the code in order to improve performance, which will lead to inconsistent results of the data as expected (reordering)
3. JMM visibility solution
Working memory of the thread
Working memory abstracted by JMM (thread local memory)
Variables stored in the thread stack, such as local variables, method parameters, exception handling parameters, etc.
CPU cache
Thread, working memory, JMM and main memory
From the above, it can be seen that in the JVM running data area, working memory and main memory interact with each other through the JMM model specification, so the visibility of data variables can be provided through the memory semantic specification defined by JMM.
Solution based on caching problem
The JMM specification stipulates that when the targeted technical means are used, the thread will be forced to read the shared data of the main memory directly bypassing the working memory.
Common technical means: volatile/synchronized/final/ has operation instructions for memory synchronization
Reorder
Follow the rules
As-if-serial: that is, no matter how much reordering (compiler and processor to improve parallelism), the execution result of the (single-threaded) program cannot be changed. Both the compiler / runtime/ processor must follow the as-if-serial semantics, that is, the compiler and the processor will not reorder operations that have data dependencies
Reordered classification
Compiler reordering: based on the semantics of a single thread program, Java enables server mode (not supported by clinet) to optimize the compilation of program code
Processor reordering: the processor can change the order in which machine instructions are executed without data dependencies
Reorder solution
The compiler forbids reordering based on a specific type of JMM (synchronous code flags, etc.)
Insert a specific memory barrier to prevent processor reordering before the Java compiler generates instructions
Memory barrier types: see CPU cache and memory barrier
This is the answer to the analysis question on the visibility of Java memory model. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.