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 into memory and removing variables from memory in the virtual machine. The variables here differ from those described in Java programming, which include instance fields, static fields, and elements that make up the array object. However, local variables and method parameters are not included.
The Java memory model specifies that all variables are stored in main memory, that each thread has its own working memory, that the thread's working memory holds a copy of the master memory of the variable used by the thread, and that all the operations on the variable must be made in working memory, and not directly read and write to the variables in main memory. The variables in the other's working memory cannot be accessed between different threads, and the transfer of variable values between threads needs to be done through main memory.
The interaction between threads, main memory, and working memory:
Inter-memory interaction operations
The Java memory model defines a 8 operation to complete the specific interaction protocol between main memory and working memory, that is, how a variable is copied from main memory to working memory, how to synchronize from the working memory back to main memory, and so on. Virtual machine implementations must ensure that each of the following volumes is atomic and non-divided.
1. Lock: A variable acting on the main memory, which represents a variable as a thread-exclusive state.
2. Unlock: A variable that acts on the main memory, releasing a variable that is in a locked state, and the released variable can be locked by another thread.
3. READ: A variable that acts on the main memory, which transfers the value of a variable from main memory to the working memory of the thread for subsequent loadd actions to be used.
4. Load: A variable that acts on the working memory, which puts the value of the variable that the read operation obtains from main memory into a variable copy of the working memory.
5. Use: A variable acting on the working memory, which 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 use the value of the variable.
6. Assign: A variable that acts on the working memory, it assigns a variable received from the execution engine to the working memory, and performs this operation whenever the virtual opportunity is given to a byte-code instruction that assigns a value to a variable.
7. Store: A variable that acts on the working memory, which assigns a variable that is received from the execution engine to the working memory, and performs this operation whenever the virtual opportunity is given to a byte-code instruction that assigns a value to a variable.
8. Write: The variable that acts on the main memory, it puts the value of the variable that the store operation obtains from the working memory into the variable of main memory.
The semantics of volatile
1. Ensure the visibility of this variable to all threads
2. Prohibit Order reordering optimization
Step-by-step learning Jvm-java memory model