In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to understand Java memory model". In daily operation, I believe many people have doubts about how to understand Java memory model. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how to understand Java memory model"! Next, please follow the small series to learn together!
Why have a memory model?
To answer this question, we need to understand the memory architecture of traditional computer hardware. All right, I'm going to start drawing.
Hardware Memory Architecture
(1)CPU
Students who have been to the computer room know that there are generally multiple CPUs configured on large servers, and each CPU will have multiple cores, which means that multiple CPUs or multiple cores can work simultaneously (concurrently). If you start a multithreaded task using Java, chances are that each CPU will run a thread, and your task will be truly concurrent at some point.
(2)CPU Register
CPU Register is the CPU Register. CPU registers are integrated within the CPU, and operations performed on registers are orders of magnitude more efficient than on main memory.
(3)CPU Cache Memory
CPU Cache Memory is also CPU cache memory, relative to registers, usually can also become L2 cache. Memory reading efficiency is very high relative to hard disk reading speed, but it is still orders of magnitude different from CPU, so multi-level cache is introduced between CPU and main memory, in order to buffer it.
(4)Main Memory
Main Memory is main memory, which is much larger than L1 and L2 caches.
Note: Some high-end machines also have L3 cache.
Cache coherence issues
Because there is an order of magnitude difference between the computing power of main memory and CPU processor, cache memory will be introduced in traditional computer memory architecture as a buffer between main memory and processor. CPU will put commonly used data in cache memory, and after the operation, CPU will synchronize the operation results to main memory.
Using cache solves the CPU and main memory rate mismatch problem, but introduces another new problem: cache coherency.
In multi-CPU systems (or single-CPU multi-core systems), each CPU core has its own cache, and they share the same Main Memory. When multiple CPU computing tasks involve the same block of main memory area, the CPU will read data into the cache for computing, which may lead to inconsistent cache data.
Therefore, each CPU needs to follow a certain protocol when accessing the cache, and operate according to the protocol when reading and writing data, so as to jointly maintain the consistency of the cache. Such protocols include MSI, MESI, MOSI, and Dragon Protocol.
Processor optimization and instruction reordering
Cache is added between CPU and main memory to improve performance, but cache coherency issues may be encountered in multithreaded concurrency scenarios. Is there any way to improve CPU performance? The answer is processor optimization.
In order to maximize the use of the processor's internal arithmetic units, the processor will perform out-of-order processing on the input code, which is processor optimization.
In addition to processor optimizations, compilers of many modern programming languages do similar optimizations, such as Java's just-in-time compiler (JIT), which does instruction reordering.
Processor optimization is also a type of reordering. To summarize, reordering can be divided into three types:
Compiler-optimized reordering. The compiler can rearrange the execution order of statements without changing the semantics of a single-threaded program.
Instruction level parallel reordering. Modern processors employ instruction-level parallelism to overlap multiple instructions. If there are no data dependencies, the processor can change the order of execution of the machine instructions corresponding to the statement.
Reordering of memory systems. Because the processor uses caches and read-write buffers, this makes load and store operations appear likely to be performed out of order.
Problems with concurrent programming
The above talks about a bunch of hardware-related things, some students may be a little confused, around such a big circle, these things have anything to do with Java memory model? Don't rush. Let's look down slowly.
Students familiar with Java concurrency will be familiar with these three problems: the visibility problem, the atomicity problem, and the ordering problem. If you look at these three problems from a deeper level, they are actually caused by the "cache coherency","processor optimization" and "instruction reordering" mentioned above.
Cache coherency problems are actually visibility problems, processor optimization may cause atomicity problems, instruction reordering may cause ordering problems, you see if they are all connected.
Problems always have to be solved, so what can be done? First of all, I thought of a simple and rude way. Killing the cache and letting the CPU interact directly with the main memory solved the visibility problem. Prohibiting processor optimization and instruction reordering solved the atomicity and ordering problems. However, it was obviously not advisable to return to the pre-liberation overnight.
Therefore, the technical predecessors thought of defining a set of memory models on physical machines to standardize memory read and write operations. Memory models solve concurrency problems in two main ways: limiting processor optimization and using memory barriers.
Java memory model
The same memory model specification may be implemented differently in different languages. The Java memory model is implemented in Java.
Java runtime memory area versus hardware memory
Students who have learned about JVM know that JVM runtime memory area is fragmented, divided into stacks, heaps, etc., in fact, these are logical concepts defined by JVM. In traditional hardware memory architecture, there is no such concept as stack and heap.
As you can see from the diagram, stacks and heaps exist in both cache and main memory, so there is no direct relationship between the two.
Java Thread and Main Memory
The Java memory model is a specification that defines many things:
All variables are stored in Main Memory.
Each thread has a private Local Memory that stores copies of shared variables that the thread can read/write to.
All thread operations on variables must be performed in local memory, and cannot read or write directly to main memory.
Threads cannot directly access variables in each other's local memory.
It was boring to read the text, so I drew another picture:
inter-thread communications
If two threads are both operating on a shared variable, the shared variable is initialized to 1, each thread increments the variable by 1, and the expected value of the shared variable is 3. There are a number of operations under the JMM specification.
In order to better control the interaction between main memory and local memory, the Java memory model defines eight operations to implement:
Lock: Locked. A variable that acts on main memory to identify a variable as a thread-exclusive state.
unlock: unlock. Act on main memory variables, releasing a locked variable so that it can be locked by other threads.
Read: Read. Act on main memory variables to transfer a variable value from main memory to the thread's working memory for use in subsequent load actions
Load: Loading. A variable that acts on working memory and places the variable value obtained from main memory by the read operation into a copy of the variable in working memory.
Use: Use. A variable that acts on working memory, passing the value of a variable in working memory to the execution engine, which executes this operation whenever the virtual machine encounters a bytecode instruction that requires the value of the variable.
assignment: assignment. A variable that acts on working memory. It assigns a value received from the execution engine to a variable in working memory. The virtual machine performs this operation whenever it encounters a bytecode instruction that assigns a value to the variable.
Store: Storage. A variable that acts on working memory to transfer the value of a variable in working memory to main memory for subsequent write operations.
Write: Write. A variable that acts on main memory to transfer the store operation from the value of a variable in working memory to a variable in main memory.
Note: working memory means local memory.
At this point, the study of "how to understand Java memory model" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
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.