Don't just know JVM but not JMM

JAVA Memory Model--JMM

Memory model concept: process abstraction for read and write access to a specific memory or cache under a specific operating protocol

JMM: The main reason is that the CPU's multi-core multi-level cache reorders instructions to optimize the code,  so the processor will reorder the code. Ensure eventual concurrency safety. JMM is an abstract concept that abstracts the concept of working memory stored in main memory, and specifies some operations to realize the interaction between variables in main memory, variables in threads , and variables between threads.

 

  1. - Variables in main memory are shared
  2. Variables between threads cannot interact but can interact in disguise through the main memory
  3. Each variable has its own working memory, which is a copy of the main memory that holds the variables used by the thread
  4. Thread operations on variables are all done in the working memory

Some operations are stipulated ☞ (eight atomic operations):

  1. - Lock operation (lock the variable of the main memory for use by the currently locked thread)
  2. Unlock operation (whoever locks will release the lock)
  3. The Read operation reads the value of the variable in the main memory to the working memory (currently it is only obtained but not put into the variable of that thread)
  4. The Load operation loads the value read by read into the variable copy of the working memory, so the variable value of the main memory variable is transferred to the variable copy of the thread working memory according to two action relays (read+load).
  5. The Use operation sends the obtained variable copy value to the execution engine (the execution engine is a thing that calculates the value of the variable and outputs a new value)
  6. The Assign assignment operation returns the value given by the execution engine to the variable copy
  7. The Store operation transfers the value of a copy of a variable in working memory to a variable in main memory
  8. The Write operation writes the value obtained by the store into the main memory variable

 

Rules for interaction between memory

 

 

Reorder:

What it is: It is a means for the compiler or processor to rearrange the order of instruction execution in order to optimize the performance of the program

Purpose: To optimize the execution performance of the program

Classification: Compiler optimization reordering (the compiler can rearrange the execution order of statements without changing the semantics of the program running in a single-threaded environment --- Purpose: Minimize the reading of registers, the number of storage times, and reuse register stored data)

Instruction-level parallel reordering (the processor executes multiple instructions in parallel, and if there is no data dependency, the processor can change the execution order of the instructions corresponding to the statement)

Reordering of the memory system (processors use cache and read and write buffers to make data loading and storage operations seem to be executed randomly)

 

as-if-serial rule

Regardless of whether there is reordering or not, and it does not care about how to reorder in a single-threaded environment, the execution result of the program will not be changed

The compiler, JVM, and processor all abide by this semantics

happens-before rule

What it is: Used to ensure the visibility between two operations and ensure the orderly running of multiple threads

include:

  1. Program sequence rules: It means that any previous operation does not necessarily have to be in front of the subsequent operation, but the result of the previous operation must be visible to the subsequent operation, which means that two people run AB in a race, and A does not necessarily run B before B. You can run first , but A must reach the end point first to get the result. If you are not satisfied, you cannot reorder
  2. Lock rules: the unlocking of the lock must be before the locking of the lock
  3. Rules for Volatile variables: A write operation to a volatile-modified property happens-before  any subsequent read operation on a field of this volatile-modified property
  4. Transitive A happens-before  B B happens-before  C So A happens-before  C
  5. Start rule: If the start of thread B is executed in thread A, then the B.start operation in thread A happens-before  any operation in thread B (a thread must start first if it wants to execute the operation in thread B)
  6. Join rule: If thread A executes the join method of thread B, the priority is higher than any operation in thread B.

Memory barrier (JMM's eight atomic actions ensure that the code of concurrent semantic keywords can achieve corresponding safe concurrent access)

Official view:

 My own simple understanding:

The memory barrier is the last layer of support for concurrency above the hardware, under the operating system or JVM.  This thing is an instruction operation to prevent some things from happening.

Classification of memory barriers in the JVM (just understand)

 

 Welcome to study and exchange, please point out the shortcomings, if you like it, please like + bookmark, thank you guys

Guess you like

Origin blog.csdn.net/m0_67601895/article/details/129137871