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 of storing variables in the virtual machine to 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. Naturally there is no question of competition.
??
To achieve better execution efficiency, theJava 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 immediate compiler's ability 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), and the thread's working memory holds a copy of the Master memory copies of the variables used by the thread, all of the thread-to- variable operations ( read, assign, etc. ) must be in working memory and not directly read and write to 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 theJava 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, it releases a variable that handles the state of the lock, and the released variable can be locked by another threadunlock The variable value must be synchronized back to main memory before.
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 in sequence operation.
??
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 , betweenstore and write , other instructions can be inserted, and if the variable A, a, and A/ b is accessed by the master, One possible order is read a,readb,loadb,load a.
??
In addition to this, theJava memory model also stipulates that the following rules must be met when performing the above eight basic operations:
??
1) One of the read and load,store , and write operations is not allowed to appear separately, that is, one variable is not allowed to read from main memory but the working memory is not accepted. or a situation in which the primary memory is not accepted when initiating writeback from the working memory.
2) A thread is not allowed 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 is "Born" only in main memory and does not allow direct use of a variable that is not initialized (load and assign) in working memory, in other words a variable is implemented in the assign and load operations must be performed before the operation.
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 Chapter 12th memory model