In-depth understanding of Java Virtual Machine notes---memory model

Source: Internet
Author: User

main memory vs. working memory
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 such as storing variables in the virtual machine into memory and extracting variable values from memory. The variable here (Variable) is slightly different from the variable described in Java compilation, which includes instance fields, static fields, and elements that make up the array object, but does not include local variables and method parameters, because the latter is thread-private and will not be shared, and there is no question of competition in nature. For better execution efficiency, the Java memory model does not restrict the execution engine from using the processor's specific registers or caches to interact with the main memory, nor does it limit the ability of the immediate compiler to adjust the order of code execution.
The Java memory model specifies that all variables are stored in the main memory. Each thread also has its own working memory (working memories), the working memory of the thread holds the copy of the Master memory copies of the variables used by the thread, and all operations of the thread on the variable (read, assign, etc.) must be in working memory, and cannot be read directly to the variables in main memory. There is no direct access to the variables in the other's working memory between different threads, and the transfer of the variable values between the threads needs to be done through main memory, and the interaction between the thread, main memory, and working memory are as follows:


Inter-memory interaction operations
The following 8 actions are defined in the Java memory model for implementation details such as how a variable is copied from main memory to working memory, and how to synchronize from the working memory back to main memory.
1) Lock: Acts on the main memory variable, which identifies a variable as a thread-exclusive state.
2) Unlock (unlocked): Acts on the main memory variable, which releases a variable that handles the locked state, and the released variable can be locked by another thread.
3) Read: Acts on the main memory variable, which transfers the value of a variable from main memory to the working memory of the thread for subsequent load actions.
4) Load (load): Acts on the working memory variable, which puts the value of the read operation from the main memory into a variable copy of the working memory.
5) Use: A variable acting on a working memory that passes the value of a variable in the working memory to the execution engine, which is performed whenever the virtual opportunity is to a bytecode instruction that needs to be used to a variable.
6) Assign (Assignment): acts on the working memory variable, which assigns a value from the execution engine to the working memory variable, which is performed whenever the virtual opportunity is assigned 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 the main memory for subsequent write operations.
8) Write: A variable that acts on the main memory, which puts the store operation's value from the working memory into a variable in the main memory.

If you want to copy a variable from main memory to working memory, perform the read and load operations sequentially, and if you want to synchronize the variables from the working memory back to the main memory, execute the store and write operations sequentially. The Java memory model simply requires that the two operations must be executed sequentially, without guarantee that it must be sequential. That is, between read and load, store and write can be inserted into other instructions, if the main variable, a, a, a, a possible order is read a, readb, LOADB, load a. In addition to this, the Java memory model also stipulates that the following rules must be met when performing the above eight basic operations:
1) does not allow one of the read and load, store, and write operations to appear separately, that is, a variable is not allowed to read from the main memory but the working memory is not accepted, or from the working memory to initiate writeback but the main memory is not acceptable.
2) does not allow a thread to discard its most recent assign operation, where the variable is changed in working memory (assigning value to a working memory variable) and must be synchronized back to main memory.
3) A new variable can only be "born" in main memory and not allow direct use of a variable that is not initialized (load and assign) in working memory, in other words a variable must have performed the assign and load operations before implementing the use and store operations.
4) If a variable is not previously locked by the load operation, it is not allowed to perform a unlock operation on it: it is also not allowed to unlock a variable that is locked by another thread.
5) This variable must be synchronized back to main memory (execute store and write operation) before executing unloack on a variable

In-depth understanding of Java Virtual Machine notes---memory model

Related Article

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.