Java memory model

Source: Internet
Author: User

The Java Virtual Machine specification attempts to define a Java memory model (MODEL,JMM) that masks memory access differences between various hardware and operating systems to enable Java programs to achieve consistent memory access across various platforms.

The main goal of the Java memory model is to define the access rules for each variable in the program, that is, the underlying details of storing variables in the virtual machine into memory and removing variables from memory.

The Java memory model specifies that all variables are stored in main memory. Each thread also has its own working memory, and the thread's working memory holds a copy of the main memory copies of the variables used by the thread.

With regard to the specific interaction protocol between main memory and working memory, the following 8 operations are defined in the Java memory model, and the virtual machine implementation must ensure that each of the operations mentioned below is atomic and non-divided.

1. Lock (Lock): A variable that acts on the main memory, which identifies a variable as a thread-exclusive state.

2. Unlock (unlock): A variable that acts on the main memory, frees a variable that is locked, and the released variable can be locked by another thread

3. READ: A variable acting on the main memory that transfers the value of a variable from main memory to the working memory variable copy of the thread

4. Load (load): A variable that acts on the working memory, which puts the value of a read operation from the main memory into a variable copy of the working memory

5. Use: A variable that acts on the working memory, which passes a variable value in the working memory to the execution engine, which is performed whenever the virtual opportunity is to a bytecode instruction that needs to use the value of the variable.

6, assign (Assignment): A variable acting on the working memory, which assigns a value from the execution engine to the working memory variable, which is performed whenever the virtual opportunity is to a byte-code instruction that assigns a value to a variable

7. Store: A variable acting on a working memory that transfers the value of a variable in the working memory to main memory for subsequent write operations to use

8. Write (write): A variable that acts on the main memory, which puts the value of the store operation's variable from the working memory into the variable of the main memory

If you want to copy a variable from main memory to working memory, then perform the read and load operations sequentially, and if you synchronize the variables from the working memory back to the main memory, you should perform the store and write operations sequentially. The Java memory model requires only that the two operations above must be executed sequentially, without guaranteeing continuous execution.

8.1 for volatile special rules for type variables

When a variable is defined as volatile, it will have two characteristics, the first is to ensure that the variable visibility of all threads, where the visibility refers to when a thread modifies the value of this variable, the new value for other threads can be immediately known. The normal variable cannot do this, the value of the normal variable is passed through the main memory to complete.

Since volatile variables can only guarantee visibility, we still need to lock to ensure atomicity in an operation scenario that does not conform to the following two rules.

1. The result of the operation does not depend on the current value of the variable, or can ensure that only a single thread modifies the value of the variable

2. Variables do not need to participate in invariant constraints with other state variables

The second semantics of using volatile variables is to prohibit command reordering optimizations, and ordinary variables will only guarantee that the correct results will be obtained in the execution of the method in all places that depend on the result of the assignment, without guaranteeing that the order of the variable's assignment is consistent with the order of execution in the program code.

Java memory model

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.