Home Java javaTutorial Detailed introduction to JVM reordering in JAVA

Detailed introduction to JVM reordering in JAVA

Jan 23, 2017 am 10:20 AM

In concurrent programs, programmers will pay special attention to data synchronization between different processes or threads. Especially when multiple threads modify the same variable at the same time, reliable synchronization or other measures must be taken to ensure that the data is modified correctly. Here An important principle is: don't make assumptions about the order in which instructions are executed. You cannot predict the order in which instructions between different threads will be executed.

But in a single-threaded program, it is usually easy for us to assume that instructions are executed sequentially, otherwise we can imagine what terrible changes will happen to the program. The ideal model is: the order in which various instructions are executed is unique and ordered. This order is the order in which they are written in the code, regardless of the processor or other factors. This model is called the sequential consistency model, and it is Model based on the von Neumann system. Of course, this assumption is reasonable in itself and rarely occurs abnormally in practice, but in fact, no modern multiprocessor architecture adopts this model because it is simply too inefficient. In compilation optimization and CPU pipeline, almost all involve instruction reordering.

Compile-time reordering

A typical compile-time reordering is to adjust the order of instructions to reduce the number of register reads and stores as much as possible without changing the program semantics, and to fully replicate the Use the stored value of the register.

Assume that the first instruction calculates a value and assigns it to variable A and stores it in a register. The second instruction has nothing to do with A but needs to occupy a register (assuming it will occupy the register where A is located). The third instruction The instruction uses the value of A and is independent of the second instruction. Then if according to the sequential consistency model, A is put into the register after the first instruction is executed, A no longer exists when the second instruction is executed, and A is read into the register again when the third instruction is executed, and during this process , the value of A has not changed. Usually the compiler will swap the positions of the second and third instructions, so that A exists in the register at the end of the first instruction, and then the value of A can be read directly from the register, reducing the overhead of repeated reading.

The significance of reordering for the pipeline

Modern CPUs almost all use the pipeline mechanism to speed up the processing of instructions. Generally speaking, an instruction requires several CPU clock cycles to process, and it is executed in parallel through the pipeline , several instructions can be executed in the same clock cycle. The specific method is simply to divide the instructions into different execution cycles, such as reading, addressing, parsing, execution and other steps, and place them in different components for processing. At the same time, in the execution unit EU, the functional units are divided into different components, such as addition components, multiplication components, loading components, storage components, etc., which can further realize parallel execution of different calculations.

The pipeline architecture determines that instructions should be executed in parallel, not as considered in the sequential model. Reordering is conducive to making full use of the pipeline, thereby achieving superscalar effects.

Ensure sequence

Although instructions are not necessarily executed in the order we wrote them, there is no doubt that in a single-threaded environment, the final effect of instruction execution should be the same as The effect is consistent under sequential execution, otherwise this optimization will be meaningless.

Usually, the above principles will be met whether the instruction reordering is performed at compile time or run time.

Reordering in Java Storage Model

In the Java Storage Model (Java Memory Model, JMM), reordering is a very important section, especially in concurrent programming. JMM ensures sequential execution semantics through the happens-before rule. If you want the thread executing operation B to observe the results of the thread executing operation A, then A and B must satisfy the happens-before principle. Otherwise, the JVM can perform arbitrary operations on them. Sorting to improve program performance.

The volatile keyword can ensure the visibility of variables, because operations on volatile are all in Main Memory, and Main Memory is shared by all threads. The price here is that performance is sacrificed and registers or registers cannot be used. Cache, because they are not global, visibility cannot be guaranteed and dirty reads may occur.

volatile also has the function of locally preventing reordering. Operation instructions on volatile variables will not be reordered, because if reordered, visibility problems may occur.

In terms of ensuring visibility, locks (including explicit locks, object locks) and reading and writing of atomic variables can ensure the visibility of variables. However, the implementation methods are slightly different. For example, synchronization lock ensures that data is re-read from the memory to refresh the cache when the lock is obtained. When the lock is released, the data is written back to the memory to ensure that the data is visible, while volatile variables simply read and write memory.

For more detailed introduction to JVM reordering in JAVA and related articles, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1252
24
Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles