Deep understanding of Java Memory Model (ii) reordering

Source: Internet
Author: User
Tags execution

If two operations access the same variable, and one of the two operations is a write operation, there is a data dependency between the two operations. Data dependencies are divided into the following three types:

In the above three cases, the execution result of the program will be changed as long as the order of execution of the two operations is reordered.

As mentioned earlier, the compiler and the processor may reorder the operations. Compilers and processors adhere to data dependencies when they reorder, and the compiler and processor do not change the order in which two operations with data dependencies exist.

Note that the data dependencies described here are only for the sequence of instructions executed in a single processor and the operations performed on a single thread, and data dependencies between different processors and between different threads are not considered by compilers and processors.

As-if-serial semantics

As-if-serial semantics means that no matter how you reorder (compilers and processors to improve parallelism), the execution of (single-threaded) programs cannot be changed. Compilers, runtime, and processors must adhere to as-if-serial semantics.

To comply with as-if-serial semantics, the compiler and the processor do not reorder operations that have data dependencies, because such reordering alters the execution result. However, if there is no data dependency between operations, these operations may be sorted by compilers and handlers. For specific purposes, consider the following code example for calculating the circle area:

Double pi  

= 3.14;    A
Double R   = 1.0;     B
Double area = pi * R * r;//c

The data dependencies of the above three operations are shown in the following illustration:

As shown in the figure above, there is a data dependency between A and C, and there is also a data dependency between B and C. Therefore, in the final sequence of instructions, C cannot be reordered to the front of A and B (c to the front of a and B, the result of the program will be changed). But there is no data dependency between A and B, and the compiler and the processor can reorder the execution order between A and B. The following diagram is the sequence of execution for the program:

As-if-serial semantics protects the single-threaded program and adheres to the as-if-serial semantics compiler, runtime and the processor create an illusion that the single-threaded programs are executed in the order in which they are programmed. As-if-serial semantics enables single-threaded programmers to worry about reordering and not to worry about memory visibility issues.

Program Order rules

According to Happens-before's program sequence rules, there are three happens-before relationships in the example code that calculates the area of the circle:

A Happens-before B;

B Happens-before C;

A Happens-before C;

The 3rd Happens-before relationship here is deduced from the transitivity of the Happens-before.

Here a Happens-before B, but in actual execution B can be performed before a (see the order of execution after the reordering above). As mentioned in the first chapter, if a happens-before b,jmm does not require a to be executed before B. JMM only requires that the previous action (the result of execution) be visible to the latter operation, and that the previous action precede the second operation in order. Operation A's execution result does not need to be visible to Operation B, and the execution result after the reordering operation A and Operation B is consistent with the result of operation A and Operation B executed in Happens-before order. In this case, JMM considers this reordering to be not illegal (not illegal), and JMM allows such reordering.

In the computer, software technology and hardware technology have a common goal: without changing the results of the program execution, as far as possible to develop the degree of parallelism. Compilers and processors to comply with this goal, we can see from the definition of Happens-before that JMM also follow this goal.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.