search
HomeJavajavaTutorialDetailed introduction to JVM reordering in JAVA

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
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
Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

Is Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksIs Java Truly Platform Independent? How 'Write Once, Run Anywhere' WorksMay 13, 2025 am 12:03 AM

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

Demystifying the JVM: Your Key to Understanding Java ExecutionDemystifying the JVM: Your Key to Understanding Java ExecutionMay 13, 2025 am 12:02 AM

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Is java still a good language based on new features?Is java still a good language based on new features?May 12, 2025 am 12:12 AM

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

What Makes Java Great? Key Features and BenefitsWhat Makes Java Great? Key Features and BenefitsMay 12, 2025 am 12:11 AM

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Top 5 Java Features: Examples and ExplanationsTop 5 Java Features: Examples and ExplanationsMay 12, 2025 am 12:09 AM

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.

See all articles

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor