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

Java memory model and memory semantics of locks

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "Java memory model and lock memory semantics". In daily operation, I believe many people have doubts about Java memory model and lock memory semantics. Xiaobian consulted all kinds of information and sorted out simple and easy to use operation methods. I hope to help you answer the doubts of "Java memory model and lock memory semantics"! Next, please follow the small series to learn together!

CPU and cache consistency

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

At first, it was still peaceful, but with the development of CPU technology, CPU execution speed is getting faster and faster. Since the technology of memory has not changed much, the process of reading and writing data from memory and the execution speed of CPU will be more and more different, which leads to a lot of waiting time for CPU to operate memory every time.

So, people came up with a good way to add cache between CPU and memory. The concept of caching is well known, and it is to keep a copy of the data. It is characterized by speed, small memory, and expensive.

Then, the execution process of the program becomes:

When the program is running, it copies the data needed for the operation from the main memory to the CPU cache, so the CPU can read and write data directly from its cache when calculating, and then flush the data in the cache to the main memory after the operation.

Adding cache between CPU and main memory may cause cache coherency problems in multi-threaded scenarios, that is, in multi-core CPUs, the cache contents of each core's own cache may not be consistent about the same data.

Processor optimization and instruction rearrangement

As mentioned above, adding cache between CPU and main memory can cause cache coherency problems in multithreaded scenarios. In addition to this situation, there is also a hardware problem that is more important. That is, in order to make the most of the processor's internal computing units, the processor may execute the input code out of order. This is processor optimization.

In addition to many popular processors that optimize code out of order, compilers for many programming languages have similar optimizations, such as the Java Virtual Machine's just-in-time compiler (JIT), which also reorders instructions.

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

Solution: Memory Barrier

What is Java Memory Model?

memory model

In order to guarantee the correctness (visibility, orderliness, atomicity) of shared memory, memory model defines the specification of read and write operation behavior of multithreaded programs in shared memory system. Through these rules to standardize the memory read and write operations, so as to ensure the correctness of instruction execution.

Java Memory Model (JMM) is defined by the Java Virtual Machine specification to shield Java programs from memory access differences between various hardware and operating systems, so that Java programs can achieve memory access consistency on various platforms.

The main goal of the Java memory model is to define access rules for variables in a program. The low-level details of storing variables in or fetching variables from main memory in a virtual machine. Note that the variables here are not identical to the variables we write in java programs. Variables here refer to instance fields, static fields, elements that make up an array object, but do not include local variables and method parameters (because these are thread-private).

The Java memory model includes:

Main memory: The Java virtual machine specifies that all variables (not variables in the program) must be generated in main memory. It can be compared to the physical machine's main memory mentioned above, except that the physical machine's main memory is the memory of the entire machine, while the virtual machine's main memory is a part of the virtual machine's memory.

Working memory: each thread in the java virtual machine has its own working memory, which is private to the thread. This can be compared to the aforementioned cache. The thread's working memory holds copies of the variables needed by the thread in main memory. The virtual machine stipulates that the thread's modification of main memory variables must be carried out in the thread's working memory, and cannot directly read and write variables in main memory. Threads cannot access each other's working memory. If the value of a variable needs to be passed between threads, it must be mediated through main memory.

The division of working memory and main memory is different from the division of Java heap, stack and method area. There is basically no relationship between the two. If it is barely corresponding, the main memory can be interpreted as the instance data part in the heap, and the working memory corresponds to some areas in the stack.

[4] Memory semantics of volatile

Memory semantics written by volatile:

When a variable is written, JMM updates the shared variable value from the thread's private memory to main memory and sets the value from other threads to invalid;

volatile Read Memory semantics:

When reading a variable, JMM determines whether the value in private space is invalid. If it is invalid, the thread next reads the variable from main memory.

[5] Memory semantics of locks

When a thread releases a lock, JMM flushes shared variables from that thread's local memory to main memory.

When a thread acquires a lock, JMM invalidates the thread's local memory. This makes critical section code protected by the monitor have to read shared variables from main memory.

At this point, the study of "Java memory model and lock memory semantics" 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.

Share To

Development

Wechat

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

12
Report