Understanding of the Java memory model

Source: Internet
Author: User

Java memory model

The Java memory model specifies what behavior is allowed and what behavior is prohibited in multithreaded programs. This may be a bit abstract, let's change an angle. Abstract program behavior into read and write operations, each thread has its own local variables, and shared variables exist between threads. Then after the execution of a multithreaded program, all variables will have a final value. Java memory model to determine what value is legal and what value is not legal.

The memory model does not require too strict, which can hinder many optimization methods, reduce the efficiency of program execution, but also can not be asked too loosely, because this will lead to some execution results against our intuition. For example, the reordering problem between instructions, if the instructions inside the thread is executed exactly in the order indicated in the program, and each execution of an instruction, the results of execution immediately, then it will hinder many optimization methods, but this is good for the programmer, because the programmer can easily infer the execution results of the program, The program written in this way is easily consistent with your own intentions. This memory model is called the Sequential consistency model (sequential consistency). Conversely, in order to optimize program execution efficiency, the likelihood of reordering is many, then the efficiency of the program is improved, but for the programmer, it is difficult to infer the execution results of the program. This type of memory model is called relaxed.

In this way, we are confronted with a dilemma:

    • The memory model requires strict, then the program is inefficient, but the programmer is easy to write to
    • The memory model requires a loose, then the program is highly efficient, but the programmer is not easy to write to

And the efficiency of the program, and whether the program is easy to write is very important. To solve this problem, the scientists put forward 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 the multithreaded program is correctly synchronized, then the programmer can follow the sequential consistency model to infer the execution results of the program, and the underlying memory operation implementation , can be optimized according to Relaxed Memory model.

The Java memory model contains two areas of content

    • A multithreaded program that is correctly synchronized to ensure that its execution results are consistent with the results performed under the sequential memory model
    • A limited amount of multithreaded programs that do not have the correct synchronization requirements to ensure security

The first aspect is related to Data Race free, and the second relates to the causality Requirements described later.

Data Race Free

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

    • Conflicting access (conflicting accesses)

If there are multiple threads that access 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 are met between two operations, you can say that there is a happen-before order between the two operations:

    1. Within the same thread, there are sequential operations in the program
    2. Operation at the end of the constructor with the start of the Finalize function
    3. Unlock operation with all locks of the same lock
    4. The read operation of the volatile variable and all write operations to it
    5. Write to the default value of a variable and the first action after the thread starts
    6. If thread T2 detects that the thread T1 terminates execution, then the last operation of T1 with T2 arbitrary action
    7. Initiates the operation of a thread with this line range first action
    8. If the thread T1 interrupts the thread T2, then this interrupt operation is the same as any other operation that sees T2 being interrupted.

Some of them I don't quite understand.

    • Data Race Free

All conflicting access operations have happen-before order, then this multithreaded program satisfies the data race free

    • Sync correctly

If the multithreaded program executes under the sequential conformance model, if it satisfies data race free, then the procedure is synchronized correctly.

The correct synchronization of multi-threaded programs, the results of the implementation of the sequential consistency model is consistent with the results. Carefully understand the relationship between the concepts. A bit around.

On the other hand, if the program is not synchronized correctly, the execution result is not arbitrary, it must be limited, but the limit is not too strong, because too strong will hinder the optimization. So the Java memory model uses the concept of causality Requirements.

Causality Requirements

The language specification stipulates a construction process, if through this construction process, can construct the multi-thread procedure final execution result, then this execution satisfies causality Requirements. The construction process starts with an empty collection C0, each time it adds several actions, and if all operations can be added, the construction succeeds. That

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

Where C_i is a subset of C_ (i+1). You may have noticed that the "operation can be added" before, what do you mean the operation can be added? The language specification stipulates that each Ci corresponds to an EI, and all ei must satisfy the well-formed executions. That is, if you add an action, the corresponding Ei does not satisfy the well-formed executions, then this operation cannot be added. If in the end, your multithreaded program cannot construct such an execution chain, then its execution result is illegal.

In addition, the Java memory model was originally maintained by the author of the paper by the authors of the Java memory model, which has an entry called causality Test Cases, giving some small examples so that people understand what behavior is satisfying causality Requirements, which is not satisfied. In addition, the well-known Doug Lea in the Java concurrency field also gives a JSR-133 Cookbook for Compiler writers, which provides a reference for compiler authors. However, the code is said to be too stringent in some places, and developers are still developing according to the Java language specification and the virtual machine specification.


Reference: Memory models for the Java language 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.