Daily sentence
Should be carefully observed in order to understand; Should strive to understand, in order to act. — Romain Rolland
If the profile
Java itself has garbage collector this intelligent automatic memory management function, is the multi-thread support concurrent mechanism function, communication between threads, Java data model is shared memory, in addition to the semaphore and other mechanisms/socket communication mechanism.
Concurrency in Java uses a “shared memory” model in which threads communicate by reading and writing to the common state of memory. Multiple threads cannot interact with each other by passing data directly; they can only interact by sharing variables.
The main purpose is to define the access rules for each variable in the program.
The JMM model specifies that all variables are stored in main memory and that each thread has its own working memory.
1. The working memory of the thread stores a copy of variables used by the thread (copied from the main memory). All operations on variables must be performed in the working memory of the thread instead of directly accessing variables in the main memory.
2. Threads cannot directly access variables in each other’s working memory, so variable values between threads must be transferred through the main memory.
3. Main memory mainly corresponds to the instance data part of the Java heap. Working memory corresponds to a portion of the virtual machine stack.
The system model of the Intel processor. As shown in the figure:
How main memory and working memory interact
Communication between Java threads is controlled by the Java Memory Model (JMM).
(1) The JMM determines when a thread’s write to a variable is visible to another thread.
(2) Shared variables between threads are stored in main memory
(3) The thread has a private local memory that stores copies of read/write shared variables.
(4) THE JMM provides memory visibility assurance through the interaction between local memory of each thread.
Visibility and order:
(1) Visibility: When a shared variable has copies in multiple local memory, if the local memory modifies the copy of the variable, other variables should be able to see the modified value.
(2) Orderliness: ensure the orderly execution of threads and ensure thread safety.
Working memory and memory interaction:
Lock: A variable that acts on main memory and identifies the variable as a thread-exclusive state.
Unlock: A variable that operates on main memory. It releases a locked variable so that it can be locked by another thread.
Read: Acts on a main memory variable to read a variable from main memory into working memory for subsequent load operations. (Optional)
Load: Acts on working memory to load variables read into working memory into a copy of the variables in working memory. (Optional)
Use: Applies to a working memory variable, passing the value of the working memory variable to an execution engine that will use the instruction whenever the virtual machine accesses an instruction that uses the variable
Assign: Applies to the working memory variable. Assign the value received by the execution engine to a variable in the working memory, and the virtual opportunity arises when an instruction assigns a value to the variable
Store: Passes the value of a working memory variable to main memory. (Optional)
Write: Writes the value of the store operation to a variable in main memory. (Optional)
Note:
1. Do not allow one of the read, load, Store, or write operations to appear alone
2. A thread is not allowed to discard the Assgin operation
3. Do not allow a thread to synchronize values from working memory to main memory without assgin operation
4. A new variable can only be generated in main memory
5. Only one thread can lock a variable at a time. However, a lock operation can be performed multiple times by the same thread. The variable is unlocked only when the same number of unlock operations are performed
6. If you lock a variable, the value of the variable will be emptied from the working memory. Before using the variable, the execution engine needs to perform load or Assgin to initialize the value.
7. If a variable is not locked, do not unlock it. Do not unlock a variable that is locked by another thread
8. Before performing an UNLOCK operation on a variable, synchronize the variable back to the main memory