Java thread working memory and main memory variable exchange process and volatile keyword understanding
1. The Java memory model specifies that in multi-threaded situations, threads manipulate main memory variables and require copies of the main memory variable to be copied by the thread's unique working memory. The so-called memory model here differs from what is commonly referred to as the virtual machine heap model:
2. The thread-specific working memory and process memory (main memory) are implemented through 8 atomic operations, as shown in:
Rules for atomic operations (partial):
1) The read,load must be executed continuously, but the atomicity is not guaranteed.
2) The Store,write must be executed continuously, but the atomicity is not guaranteed.
3) You cannot lose a copy of the last assign operation of the variable, that is, the copy that iterates through the last assign must be written back to Mainmemory.
Other rules see "in-depth understanding of Java Virtual Machines" chapter 12th Java memory Models and threading
3. Volatile keyword
A variable defined as a volatile type has two semantics:
1) Changes to variables are visible to all threads
Understand:
Each use variable in a thread requires successive read->load->use operations, that is, each usage updates the variable value from the main memory so that the other thread's modifications are visible to the thread.
Each time a thread assign a variable, it needs to perform several assign->store->write operations sequentially, that is, it writes back to main memory after each update so that all other threads are reading the latest data.
2) Prohibit command rearrangement
Reference:
"In-depth understanding of Java Virtual Machines" Chapter 12th Java memory model and threading
Java thread working memory and main memory variable exchange process and volatile keyword understanding