Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Knowledge summary of Java memory model

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "knowledge summary of Java memory model". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Why is there a memory model?

Before introducing the Java memory model, let's take a look at what the computer memory model is, and then look at what the Java memory model does based on the computer memory model. To talk about the memory model of a computer, it is necessary to talk about an ancient history and see why there is a memory model.

Memory model, English name Memory Model, he is a very old antique. He is a concept related to computer hardware. So let me tell you what he has to do with the hardware.

CPU and cache consistency

We should all know that when a computer executes a program, every instruction is executed in CPU, and when it is executed, it is inevitable to deal with data. And the data on the computer is stored in the main memory, that is, the physical memory of the computer.

At first, it was all right, but with the development of CPU technology, the execution speed of CPU is getting faster and faster. Because the technology of memory does not change much, the gap between the process of reading and writing data from memory and the execution speed of CPU will become larger and larger, which leads to a lot of waiting time for each operation of CPU.

This is like a startup, at first, the founders and employees have a happy working relationship, but as the founders become more and more capable and ambitious, there is a gap between the founders and their employees, and the average employee can't keep up with CEO. After every order of the boss is transmitted to the grass-roots employees, it will take a lot of time because of the lack of understanding and execution ability of the grass-roots employees. This also virtually slows down the efficiency of the whole company.

However, just because the reading and writing speed of memory is slow, we should not develop CPU technology. We can't let memory become the bottleneck of computer processing.

So, people have come up with a good idea to increase the cache between CPU and memory. As we all know, the concept of caching is to keep a copy of data. He is characterized by high speed, small memory, and expensive.

Then, the execution of the program becomes:

When the program is running, it will copy a copy of the data needed by the operation from the main memory to the CPU cache, then CPU can read and write the data directly from its cache when calculating, and then flush the data in the cache to the main memory when the operation is over.

After that, the company began to set up middle managers, managers directly under the leadership of CEO, leaders have any instructions, directly tell managers, and then you can do your own thing. The manager is responsible for coordinating the work of the staff at the bottom. Because managers know the people under them and what they are responsible for. Therefore, most of the time, the company's various decisions, notifications, etc., CEO as long as communication with managers is enough.

With the continuous improvement of the ability of CPU, a layer of cache is slowly unable to meet the requirements, and gradually derives a multi-level cache.

According to the order of data reading and the tight degree of integration with CPU, CPU cache can be divided into primary cache (L1), secondary cache (L3), and some high-end CPU also have tertiary cache (L3). All the data stored in each level cache is part of the next level cache.

The technical difficulty and manufacturing cost of these three caches are relatively decreasing, so their capacity is also relatively increasing.

Then, with multi-level caching, the execution of the program becomes:

When CPU wants to read a data, it first looks it up in the primary cache, if it doesn't find it, then looks it up in the secondary cache, and if it still doesn't, it looks in the tertiary cache or memory.

As the company gets bigger and bigger, the boss needs to take care of more and more things, the company's management department began to reform, began to appear high-level, middle-level, bottom and other managers. Level-by-level management.

A single-core CPU contains only one set of L1 Magi L2 Magi L3 caches; if CPU has multiple cores, that is, multicore CPU, then each core contains a set of L1 (or even and L2) caches and shares L3 (or L2) caches.

There are also many kinds of companies, some of which have only one big Boss, and he alone has the final say. But some companies have mechanisms such as co-general managers, partners and so on.

Single-core CPU is like a company with only one boss, and all the orders come from him, so all you need is a management team.

Multicore CPU is like a company co-founded by multiple partners, so you need to set up a set of senior managers for each partner to lead directly, and multiple partners share the bottom staff of the company.

Other companies, growing, began to differentiate their subsidiaries. Each subsidiary is more than one CPU, and they don't share resources with each other before. It doesn't affect each other.

The following figure shows a single CPU dual-core cache structure.

With the continuous improvement of computer capabilities, it began to support multithreading. Then here comes the problem. We analyze the influence of single-thread and multi-thread in single-core CPU and multi-core CPU respectively.

Single thread. The cache of the cpu core is accessed by only one thread. The cache is exclusive and there are no problems such as access conflicts.

Single-core CPU, multithreading. Multiple threads in the process will access the 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 the thread switch occurs, the cache will not fail. However, since only one thread is executing at any one time, there is no cache access conflict.

Multi-core CPU, multi-thread. Each core has at least one L1 cache. If multiple threads access a shared memory in a process, and each thread executes on a different core, each core retains a buffer of shared memory in its own caehe. Because multiple cores can be parallel, multiple threads may write their respective caches at the same time, and the data may be different between the respective cache.

Adding cache between CPU and main memory may cause cache consistency in multi-threaded scenarios, that is, in multi-core CPU, the cache content of the same data may be inconsistent in each core's own cache.

If the company's orders are issued in series, then there will be no problem.

If the company's orders are issued in parallel, and they are all issued by the same CEO, there is nothing wrong with this mechanism. Because the executor of his order has only one management system.

If the company's orders are issued in parallel, and they are issued by multiple partners, this will be a problem. Because each partner will only give orders to his or her immediate managers, while the underlying employees managed by multiple managers may be public.

For example, partner 1 wants to fire employee a, partner 2 needs to give employee an a promotion, and if he is dismissed after the promotion, he needs a meeting of multiple partners to decide. The two partners issued orders to their own managers. After the order of partner 1 was issued, manager a knew that the employee had been fired after he dismissed the employee. At this time, the manager of partner 2 thought that employee a was on the job before he got the news, so he gladly accepted the promotion order given to him by the partner.

Processor optimization and instruction rearrangement

It is mentioned above that adding cache between CPU and main memory will cause cache consistency in multithreaded scenarios. In addition to this situation, there is also a hardware issue that is also important. That is, in order to make full use of the computing units within the processor, the processor may process the input code out of order. This is processor optimization.

In addition to many popular processors that optimize the code out of order, compilers in many programming languages have similar optimizations, such as the just-in-time compiler (JIT) of the Java virtual machine.

It is conceivable that if processor optimization and compiler rearrangement of instructions are allowed, it may lead to all kinds of problems.

With regard to the organizational adjustment of employees, if the personnel department is allowed to split and rearrange at will after receiving multiple orders, it will have a great impact on the employee and the company.

The problem of concurrent programming

You may be a little confused about the concept related to hardware, and you don't know what it has to do with software. But you should know something about concurrent programming, such as atomicity, visibility, and orderliness.

In fact, atomicity, visibility and order. It is defined abstractly by people. The underlying problem of this abstraction is the cache consistency problem, processor optimization problem and instruction rearrangement problem mentioned earlier.

Here is a brief review of these three questions, not ready to go into depth, interested readers can learn by themselves. We say that concurrent programming, in order to ensure data security, needs to meet the following three features:

Atomicity means that in an operation, the cpu cannot be paused and rescheduled halfway, either the operation is interrupted, or the execution is completed or not executed.

Visibility means that when multiple threads access the same variable, one thread modifies the value of the variable, and other threads can see the modified value immediately.

Orderliness means that the order in which the program is executed is in the order of the code.

Have you found that the cache consistency problem is actually a visibility issue? Processor optimization can lead to atomicity problems. Instruction rearrangement can lead to ordering problems. Therefore, the concepts at the hardware level will not be mentioned later, but will directly use the familiar atomicity, visibility and orderliness.

What is the memory model?

As mentioned earlier, cache consistency problems and processor-optimized instruction rearrangement problems are caused by continuous hardware upgrades. So, is there any mechanism that can solve the above problems well?

The simplest and most straightforward approach is to abolish processor and processor optimization techniques, abolish CPU caching, and let CPU interact directly with main memory. However, doing so can ensure the concurrency problem in multithreading. However, this is a bit of giving up eating for fear of choking.

Therefore, in order to ensure that atomicity, visibility and order can be satisfied in concurrent programming. There is an important concept, that is, the memory model.

In order to ensure the correctness of shared memory (visibility, order, atomicity), the memory model defines the specification of read and write behavior of multithreaded programs in shared memory system. These rules are used to regulate the read and write operation of memory, so as to ensure the correctness of instruction execution. It is processor-related, cache-related, concurrency-related, and compiler-related. It solves the memory access problems caused by CPU multi-level cache, processor optimization and instruction rearrangement, and ensures the consistency, atomicity and order in concurrent scenarios.

The memory model mainly uses two ways to solve the concurrency problem: limiting processor optimization and using memory barrier. This article will not go deep into the underlying principles to introduce, interested friends can learn by themselves.

What is the Java memory model

The computer memory model has been introduced earlier, which is an important specification for solving the concurrency problem in multithreaded scenarios. So what is the specific implementation? different programming languages may have different implementations.

We know that the Java program needs to run on the Java virtual machine. The Java memory model (Java Memory Model, JMM) is a mechanism and specification that conforms to the memory model specification, shields the access differences between various hardware and operating systems, and ensures the consistent memory access of Java programs on various platforms.

The Java memory model stipulates that all variables are stored in the main memory, and each thread has its own working memory. The working memory of the thread keeps a copy of the main memory copy of the variables used in the thread. All the operations of the thread on the variables must be carried out in the working memory, and can not read or write the main memory directly. There is no direct access to variables in each other's working memory between different threads, and the transfer of variables between threads requires data synchronization between their own working memory and main memory.

JMM acts on the data synchronization process between working memory and main memory. He specifies how and when to synchronize data.

The main memory and working memory mentioned here can be simply compared to the concepts of main memory and cache in the computer memory model. In particular, it should be noted that the main memory and working memory are not the same level of memory partition as the Java heap, stack, method area, etc., in the JVM memory structure, and cannot be directly compared. According to in-depth understanding of the Java virtual machine, if it has to be matched reluctantly, from the definition of variable, main memory, and working memory, the main memory mainly corresponds to the object instance data part of the Java heap. The working memory corresponds to part of the virtual machine stack.

So, to sum up, JMM is a specification that aims to solve the problems caused by the inconsistency of local memory data when multi-threads communicate through shared memory, the reordering of code instructions by the compiler, and the out-of-order execution of the code by the processor.

Implementation of Java memory Model

Friends who know Java multithreading know that a series of keywords related to concurrent processing are provided in Java, such as volatile, synchronized, final, concurren package, and so on. In fact, these are some keywords that the Java memory model provides to programmers after encapsulating the underlying implementation.

When developing multithreaded code, we can directly use keywords such as synchronized to control concurrency, and we never need to care about the underlying compiler optimization, cache consistency and other issues. Therefore, the Java memory model, in addition to defining a set of specifications, also provides a series of primitives that encapsulate the underlying implementation for developers to use directly.

This article is not going to introduce the usage of all the keywords one by one, because there is a lot of information about the usage of each keyword on the Internet. Readers can learn by themselves. Another key point of this article is that we mentioned earlier that concurrent programming has to solve the problems of atomicity, orderliness, and consistency, so let's take a look at what methods are used to guarantee it in Java.

Atomicity

In Java, to ensure atomicity, two advanced bytecode instructions monitorenter and monitorexit are provided. For these two bytecodes, the corresponding keyword in Java is synchronized.

Therefore, synchronized can be used in Java to ensure that operations within methods and code blocks are atomic.

Visibility

The Java memory model is realized by synchronizing the new value back to the main memory after the variable is modified and refreshing the variable value from the main memory before the variable is read, which depends on the main memory as the transmission medium.

The volatile keyword in Java provides a function that modifies variables that can be synchronized to main memory immediately after they are modified, and variables that are modified are refreshed from main memory before each use. Therefore, you can use volatile to ensure the visibility of variables in multithreaded operations.

In addition to the synchronized and final keywords in volatile,Java, visibility can also be achieved. It's just that it's implemented in a different way, and it's no longer unfolded here.

Order

In Java, synchronized and volatile can be used to ensure the order of operations between multiple threads. There are differences in how it is implemented:

The volatile keyword forbids instruction rearrangement. The synchronized keyword guarantees that only one thread is allowed to operate at a time.

Well, here's a brief introduction to the keywords that can be used to solve atomicity, visibility, and ordering in Java concurrent programming. Readers may find that, as if the synchronized keyword is omnipotent, it can meet the above three features at the same time, which is actually the reason why many people abuse synchronized.

However, synchronized is relatively performance-sensitive, and although the compiler provides many lock optimization techniques, overuse is not recommended.

This is the end of the introduction of "knowledge Summary of Java memory Model". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report