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 > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to understand the Java memory model", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the Java memory model.
The Origin of memory Model
1. When the computer executes the program, every instruction is executed by CPU. While CPU is executing in order to get data, it is hard to avoid dealing with main memory.
two。 With the development of CPU technology, its execution is getting faster and faster, and because the development of memory technology is relatively slow, the performance does not change much, so it takes a certain amount of waiting time for each operation of CPU.
3. In order to optimize and solve this problem while ensuring the development of CPU technology, people later came up with a solution, that is, to add cache memory (Cache) between CPU and memory.
Cache memory is a primary memory between main memory and CPU, which is composed of static memory chip (SRAM). Its capacity is relatively small but its speed is much higher than that of main memory, which is close to the speed of CPU. In the hierarchical structure of computer storage system, it is a high-speed and small-capacity memory between the central processing unit and the main memory. It forms a first-level memory together with the main memory. The scheduling and transmission of information between cache memory and main memory is carried out automatically by hardware.
Because the speed of Cache is close to that of CPU and CPU accesses Cache before each operation of main memory, the optimization effect is achieved by adding Cache.
4. With the upgrade of CPU, one-tier cache is slowly unable to meet the requirements, so multi-level cache is gradually derived. All data stored in each level of cache is part of the next level of cache. The CPU read data has also evolved into: when CPU wants to read a data, it first looks from the first-level cache, if not found, then from the second-level cache, if still not, from the next level of cache until the memory is accessed. As shown in the following figure
5. The single-core CPU contains only a set of L1 CPU L2 L3 caches. If the CPU contains multiple cores, that is, multi-core L3 caches, each core contains a set of L1 (or even and L2) caches, and shares L3 (or L2) caches. The following figure shows a single CPU dual-core cache structure:
As computers continue to improve and begin to support multithreading, there may be problems. We analyze the influence of single-thread and multi-thread in single-core CPU and multi-core CPU respectively.
Single-core cpu and single-thread: the core cache is accessed by only one thread, the cache is exclusive, and there are no access conflicts.
Single-core CPU and multithreading: multiple threads in a process will access shared data in the process at the same time. After CPU loads a block of memory into the cache, different threads will map to the same cache location when accessing the same physical address, so that even if thread switching occurs, the cache will not fail. However, since only one thread is executing at any one time, there is no cache access conflict.
Multicore CPU and multithreading: each core has at least one L1 cache for efficiency. When multiple threads execute on different cores and access the same shared memory in the process, because multi-cores can be parallel, concurrency problems similar to those in multithreaded programming may occur, such as for variables in the same memory, if multiple cores read and write and modify data at the same time, unexpected errors will occur, and the solution is through the locking mechanism.
Therefore, adding cache between CPU and main memory may lead to ambiguity when concurrent memory access operations occur in multi-core CPU multithreading scenarios.
Processor optimization-"instruction rearrangement"
In addition to the above problem, there is another hardware problem that is also important: the processor may execute the processing input code out of order in order to make full use of its internal computing units, which is temporarily understood as "instruction rearrangement". In addition to some processors will optimize the code out of order, many programming language compilers will have similar optimizations, such as Java virtual machine JIT just-in-time compiler will also do instruction rearrangement.
For example, the application of the voliate keyword in the Java singleton design pattern Double-Check example is to prevent exceptions in multi-thread concurrency scenarios caused by instruction rearrangement. Interested friends can refer to another article of my in-depth analysis of the singleton pattern-the lazy pattern, which will no longer be discussed too much here.
What is the memory model?
The purpose of all the above analysis is to raise two important questions:
Ambiguity may occur when concurrent memory access operations occur in multi-core CPU multithreading scenarios
The processor optimizes itself in order to make full use of its internal computing units-- "instruction rearrangement".
So how to solve the above problems? This leads to an important concept, the memory model, which is defined as follows:
The memory model is an abstraction of the process of reading and writing access to memory, which can be understood as the memory model defines the norms of read and write operations in the shared memory system, through these rules to regulate the read and write operations to memory, so as to ensure the correctness of instruction execution. It is processor-related, cache-related, concurrency-related, and compiler-related. The purpose is to solve the problems and ambiguities caused by CPU multi-level cache, processor optimization, instruction rearrangement and so on.
It can be simply understood that the memory model is actually an important specification to solve the problems caused by concurrency in multi-threaded scenarios.
Java memory model
1. Define
Java memory model (Java Memory Model, JMM) is a specification that conforms to the memory model specification and shields the access differences of various hardware and operating systems, in order to achieve the consistent memory access effect of Java programs on various platforms.
two。 Understand
Java memory model (JMM for short) is a specification, its main purpose is to define the access rules of various variables in the program, and is built around how to deal with atomicity, visibility and ordering in the process of concurrency.
On the implementation of the Java memory model, I believe friends familiar with Java concurrent programming will be familiar with, Java provides a series of keywords related to concurrent processing, in fact, these are some keywords that the Java memory model encapsulates the underlying implementation for programmers to use, this article will not discuss these keywords one by one, interested friends can take a look at the "Java multithreaded programming core technology" to understand. Among them are:
Resolve problems caused by instruction rearrangement by using the volatile keyword
Use synchronized keyword to ensure thread safety, etc.
In fact, it is consistent with the idea that the hardware solves the problem by limiting processor optimization and using memory barrier in the computer memory model.
At this point, I believe you have a deeper understanding of "how to understand the Java memory model". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.