Java memory Model
(1). Main memory and working memory
The Java memory model specifies that all variables are stored in main memory.
The main memory copy of a variable of each type of thread, all operations of the thread on the variable (read operation, assignment operation, etc.) must be in working memory, and cannot be read directly to the variables in main memory.
There is no direct access to variables in the other's working memory between different threads, and the transfer of variables between threads needs to be done through main memory.
(2) Memory amount operation
Main memory interacts with working memory, and the Java memory Model defines 8 operations to complete.
A) Lock: A variable that acts on the main memory, which identifies a variable as a linear exclusive state.
b) Ublock (UNLOCKED): 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.
c) Read: A variable that acts on the main memory, which transfers the value of a variable from main memory to the thread's memory for subsequent load actions to be used.
d) Load (load): A variable that acts on memory, which places the value of a read operation from the main memory into a variable copy of the working memory.
e) Use: A variable in 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 use the value of the variable.
f) Assign (Assignment): A variable in the working memory that assigns the value received by an execution engine to a variable in the working memory, and performs this operation whenever the virtual opportunity is assigned to a byte-code instruction that assigns a value to a variable.
g) Store: A variable acting on a working memory that passes the value of a variable in the working memory to the main memory for subsequent write operations to use.
h) Write: A variable that acts on the main memory, which puts the value of the variable that the store operation obtains from the working memory into a variable in the main memory.
(3) The Java Memory model stipulates that the following rules must be met when performing the above 8 basic operations:
A) does not allow one of the read and Load,store and write operations to appear separately, allowing a variable to be read from main memory but not accepted by the working RAM initiating writeback
b) does not allow a thread to discard its most recent assign operation, where the variable must be synchronized to the main memory after it has changed in working memory
c) do not allow a thread for no reason (no assign operation has occurred) to synchronize the data from the working memory of the thread to the main memory
d) A new variable can only be "born" in main memory, does not allow direct use of an uninitialized variable in working memory, in other words, the Assign and load operations must be performed before the Use,store operation is implemented on a variable
e) A human variable at the same time allows only one thread to do the lock operation can be repeated multiple times by the same thread, multiple execution of lock, only perform the same del unlock operation, the variable will be unlocked
f) If a variable performs a lock operation, it will empty the value of this variable in the working memory and need to re-execute the value of the load or assign operation initialization variable before the execution engine uses the variable
g) If a variable is not locked by the lock operation in advance, it is not allowed to perform a unlock operation on it, nor is it allowed to unlock a variable locked by another thread
h) before performing a unlock operation on a variable, you must first synchronize this variable to boil the main memory (perform store,write operation)
(4) Special rules for volatile variables:
A) can only guarantee visibility
b) prohibit command reordering optimization
(5) Special rules for variables of long and double types:
A) The Java memory model stipulates that the virtual machine is allowed to divide the read and write operations of 64 bits of data that are not volatile modified into two 32-bit operations, and allows the virtual machine implementation to select the atomicity of the 4 operations that do not guarantee the 64-bit data type load,store,read and write. This is the non-atomic protocol for long and double.
(6) Atomicity, visibility and ordering
Atomicity: Atomic variable operations that are directly guaranteed by the Java memory model include Read,load,assign,use,store and write
Visibility: When a thread modifies the value of a shared variable, other threads can immediately know the change
Order: If you observe within this thread, all operations are orderly, and if you observe another thread in one thread, all operations are unordered
(7) The principle of antecedent occurrence
First occurs when the relationship between two operations defined in the Java memory model, if operation a first occurs in operation B, is actually before the operation B, the effect of operation A can be observed by Operation B. "Impact" includes modifying the values of shared variables in memory, sending messages, calling methods, and so on.
Program Order rules: In a thread, according to the sequence of program code, written in front of the operation before the occurrence of the writing in the back. To be precise, you should control the flow order rather than the program code order, because you want to consider the structure of branching, looping, and so on.
Pipe lock rule: A unlock operation occurs after the lock operation that faces the same lock.
Volatile variable rule: The write operation of a volatile variable precedes the read operation of the variable, where the "back" refers to the order of time.
Thread Start rule: The Start method of the Thread object takes precedence over every action of this thread
Thread termination rule: All operations in a thread are preceded by termination detection for this thread, and we can end with the Thread.Join () method. The return value of Thread.alive () is checked until the thread has terminated execution
Thread break rule: the invocation of the thread interrupt () method occurs when the code of the interrupted thread detects that the interrupt event occurred, and can be detected by the thread.interrupted () method
Object Finalization rule: Initialization of an object (the end of the execution of a constructor) occurs first at the beginning of its finalize () method
Transitivity: If operation a precedes operation B, Operation B first occurs in Operation C, it is concluded that operation a precedes operation C.
jvm-Concurrent-java memory model