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

The principle and usage of Java memory model

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

Share

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

Aiming at the principle and usage of Java memory model, 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.

Why is there a memory model?

Before we introduce 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 to see why there is a memory model.

Memory model: English name Memory Model, it is an antique. It is a concept related to computer hardware. So, let me first introduce what it has to do with hardware.

CPU and cache consistency

We should 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.

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.

It's like a startup, where founders and employees have a happy working relationship at first, but as founders become more capable and ambitious, there is a gap between founders and employees, and ordinary employees are less and less able to keep up with CEO.

After every order of the boss is communicated to the grass-roots employees, it will take a lot of time due to the lack of understanding and execution ability of the grass-roots employees. This also virtually slows down the efficiency of the whole company.

However, we should not stop developing CPU technology just because the reading and writing speed of memory is slow, can 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. It is characterized by high speed, small memory and high price.

In that case, the execution process 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 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. So most of the time, the company's various decisions, notifications, etc., CEO as long as the communication between 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 (L2), and some high-end CPU also has tertiary cache (L3). All the data stored in each level of cache is part of the next level of cache.

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

Then, after having a multi-level cache, the execution of the program becomes: when CPU wants to read a data, it first looks in the first-level cache, if it doesn't find it, then looks in the second-level cache, and if it still doesn't, it looks in the third-level 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 L1PowerL2 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 and 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 spin off 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 the problem arises, let's analyze the impact 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 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.

Multi-core CPU, multi-threading: 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 Cache.

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 are abstractly defined 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, 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 then rescheduled, that is, the operation is not interrupted, either it is completed or it is 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, that is, the order in which the program is executed 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 consistency, atomicity and order in concurrent scenarios.

The memory model mainly uses two ways to solve the concurrency problem:

Limit processor optimization

Use the 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 is introduced earlier, which is an important specification to solve the concurrency problem in multithreaded scenarios.

So what is the specific implementation? Different programming languages may have different implementations.

We know that Java programs need to run on Java virtual machines. Java memory Model (Java Memory Model,JMM) is a mechanism and specification that conforms to the memory model specification, shields the access differences of various hardware and operating systems, and ensures that Java programs can access memory in various platforms.

When it comes to the Java memory model, it generally refers to the new memory model that JDK 5 began to use, mainly described by JSR-133:JavaTM Memory Model and Thread Specification.

If you are interested, you can refer to this PDF document:

Http://www.cs.umd.edu/~pugh/java/memoryModel/jsr133.pdf

The Java memory model specifies that all variables are stored in 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, and all operations on the variables must be carried out in the working memory, rather than reading and writing 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. It specifies how and when to do data synchronization.

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.

"in-depth understanding of the Java virtual machine" holds that: 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 solves 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.

The goal is to ensure atomicity, visibility, and order in concurrent programming scenarios.

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, so we don't need to care about underlying compiler optimization, cache consistency and so on.

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.

As we mentioned earlier, concurrent programming solves the problems of atomicity, order, and consistency. 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.

In the article on the implementation principle of Synchronized, it is introduced that the corresponding keyword of these two bytecodes 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 being modified.

The variables modified by it are refreshed from the 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.

The implementation is different: 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 all three features at the same time, which is 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 answer to the question about the principle and usage of the 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.

Share To

Development

Wechat

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

12
Report