Java Memory Model

Source: Internet
Author: User

Java Memory Model
Java Memory Model

The Java Memory Model specifies the behaviors that are allowed and prohibited in multi-threaded programs. In this case, it may be a bit abstract. Let's change the angle. Abstract program behavior into read and write operations. Each thread has its own local variables and shared variables exist between threads. After a multi-threaded program is executed, all variables have a final value. The Java memory model determines what value is valid and what value is invalid.

The memory model cannot be too strict, which will impede many optimization methods and reduce the efficiency of program execution, but it cannot be too loose, because it will lead to some execution results in violation of our intuition. For example, if the commands in the thread are executed in the order specified in the program and each command is executed, the execution result takes effect immediately, this will impede many optimization methods, but it is good for programmers, because programmers can easily infer the execution results of the program, so that the written program is easy to be consistent with their own intentions. This memory model is called Sequential Consistency ). On the contrary, if there are many possibilities for re-sorting to optimize the program execution efficiency, the program efficiency is improved, but it is difficult for programmers to deduce the program execution results. This type of Memory Model is called the Relaxed Memory Model.

In this way, we have encountered a dilemma:

  • The memory model requirements are strict, so the program efficiency is low, but programmers are easy to write
  • The memory model requires loose, so the program efficiency is high, but programmers cannot easily write

    The program efficiency and whether the program is easy to write are all important. To solve this problem, scientists proposed the concept of Data Race Free, which is a description of the degree of synchronization of multi-threaded programs. The basic idea is that if a multi-threaded program is correctly synchronized, then the programmer can infer the execution result of the program according to the sequence consistency Model, while the underlying implementation of Memory operations can be optimized according to the Relaxed Memory Model.

    The Java memory model contains two aspects:

    • For a correctly synchronized multi-threaded program, ensure that the execution results are consistent with those in the sequential memory model.
    • Restrict the multi-threaded programs that do not have correct synchronization requirements to a certain extent to ensure security.

      The first aspect is related to Data Race Free, and the second aspect is related to Causality Requirements described later.

      Data Race Free

      The Java memory model actually defines several concepts to illustrate what is correct synchronization.

      • Conflicting accesses)

        If there are multiple threads accessing the same address at the same time and at least one is a write operation, the program has conflicting access

        • Happen-before order

          If any of the following conditions is met between the two operations, there is a happen-before order between the two operations:

          1. In the same thread, there are sequential operations in the program
          2. Operations at the end of the constructor and operations at the beginning of the finalize Function
          3. The unlock operation is the same as all lock operations.
          4. Read operations on volatile variables and all write operations on them
          5. Write operation on the default value of the variable and the first operation after the thread starts
          6. If thread T2 detects that thread T1 is terminated, the last operation of T1 and any operation of T2.
          7. The operation to start a thread and the first operation in this thread
          8. If thread T1 interrupts thread T2, the interrupted operation is in line with any other operation that sees T2 being interrupted.

            Some of them are not very understandable ..

            • Data race free

              There is a happen-before order between all operations with conflicting access, so this multi-threaded program meets the requirements of data race free

              • Correct Synchronization

                If the multi-threaded program is executed in the sequence consistency model and the program meets the requirement of data race free, the program is correctly synchronized.

                The execution result of a correctly synchronized multi-threaded program is consistent with that in the sequential consistency model. Carefully understand the relationship between concepts. It's a bit difficult.

                On the other hand, if the program is not correctly synchronized and the execution results are not arbitrary, it must be restricted, but the restriction cannot be too strong, because it will hinder optimization. Therefore, the Java memory model uses the Causality Requirements concept.

                Causality Requirements

                To precisely define the memory model, the concept of Causality Requirements is proposed in the Java language specification. I don't know why. This concept is rarely mentioned, but I think it is very important, but it is also very confusing. In the language specification, the Well-Formed Executions concept is first defined. Many discussions on the memory model are at this layer, which includes the execution of multi-threaded programs and locks, volatile variables and execution sequence. If the execution of a multi-threaded program meets these requirements, the execution will be Well-Formed Executions. There is a series of articles in China, deep understanding of the Java memory model, which mainly describes the Java memory model. In addition, The famous Doug Lea in The field of Java concurrency also gives The JSR-133 Cookbook for Compiler Writers, which provides reference for Compiler authors and discusses this issue. However, whether the execution of a multi-threaded program by the memory model is legal depends not only on whether it is Well-Formed Executions, but also on whether the execution meets the Requirements of Causality Requirements.

                The language specification specifies a constructor. If the constructor can construct the final execution result of a multi-threaded program, then this execution will meet the Requirements of Causality Requirements. The constructor starts from an empty set C0 and adds several operations each time. If all operations can be added, the constructor is constructed successfully. That is,

                C0 -> C1 -> C2 -> ... -> C

                C_ I is a subset of C _ (I + 1. You may have noticed that "operations can be added". What is "operations can be added? According to the language specification, each Ci corresponds to an Ei, and all Ei must meet Well-Formed Executions. That is to say, if the corresponding Ei does not meet Well-Formed Executions after you add an operation, this operation cannot be added. If your multi-threaded program cannot construct such an execution chain, the execution result is invalid.

                In addition, The author of The Java Memory Model first maintained a page of The Java Memory Model. One of The entries is Causality Test Cases, which provides some small examples, so that people can understand what behaviors meet Causality Requirements and what are not satisfied. In addition, Doug Lea, well-known in The field of Java concurrency, also gives The JSR-133 Cookbook for Compiler Writers, which provides reference for Compiler authors. However, it is said that this specification is too strict in some places. Developers still develop it according to the Java language specification and virtual machine specification.

                 

                 

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.