JVMMemory organization is mainly composed of two parts: memory and working memory.
The main memory is shared by all threads, mainly including the local method area and the heap.
Each thread has a working memory not shared, and the working memory mainly consists of two parts.
1:One is a stack that belongs to the private thread of the thread;
2:Registers that contain copies of main memory variables (including program counters PC and cup cache).
1.All variables are stored in main memory (part of the virtual machine memory) and are shared for all threads.
2.Each thread has its own working memory. In the working memory, copies of some variables in main memory are stored.
All operations of threads on variables must be performed in working memory, rather than directly reading and writing variables in main memory.
3.Threads cannot directly access the variables in the working memory of each other. The transfer of variables between threads must be done through the main memory, that is, threads, main memory, and working memory.
JVMThe specification defines the intermemory interoperation between threads:
Lock(Locking): a variable that acts on the main memory and identifies a variable as a state of exclusive thread.
Read(Read: a variable that acts on the main memory and transfers the value of one variable from the main memory to the working memory of the thread.
Load(Load): the variables that are applied to the working memory, and the values of the variables obtained from the main memory of the read operation are placed in the variable copy of the working memory.
Use(Use): a variable that acts on working memory and transfers the value of one variable in the working memory to the execution engine.
Assign(Assignment): a variable that acts on working memory and assigns a value from the execution engine to the variable in the working memory.
Store(Storage): a variable that acts on working memory and transfers the value of one variable in the working memory to the main memory.
Write(Write: a variable that acts on the main memory, and puts the value of the store operation from the working memory to the variable in the main memory.
Unlock(Unlocking): a variable that acts on the main memory to release a locked variable and then be locked by other threads.
When variables are read from main memory to working memory, read and load must be executed sequentially.
To synchronize variables from working memory to main memory, store and write must be executed sequentially.
8The operation must follow the following rules:
1：One of the operations of read and load, store and write is not allowed to appear alone. That is, one variable is not allowed to read from the main memory, but the working memory is not accepted or written back from the working memory, but the main memory is not accepted.
2：One thread is not allowed to discard its latest assign operation, that is, when the working memory is changed, the variable must be changed to the main memory simultaneously.
3：Variables in working memory do not allow meaningless synchronization to return to main memory without performing assign operation.
4：Before executing use, load must be executed, and assign must be executed before executing store.
5：A variable allows one thread to perform lock operations on it at the same time, and a thread can perform multiple lock for the same variable, but the same number of unlock operations must be performed to unlock.
6：When a variable is in lock, a thread will empty the value of the variable in the working memory. The execution engine must re read and load before use.
7：The thread does not allow the lock operation of the other threads of the unlock. And the unlock operation must be after the lock operation of this thread.
8：Before executing unlock, store and write operations must be performed first.