Java Memory Model (JMM)

This article briefly introduces the Java memory model

 

1. Main memory and working memory

2. Interaction between memory

 

1. Main memory and working memory

 

1. Java Memory Model (Java Memory Model, JMM)

      The Java virtual machine specification attempts to use a Java memory model to shield the memory access differences between various hardware and operating systems , so as to achieve consistent concurrency effects for Java programs on various platforms.

 

2. Main memory and working memory

 

1> The main purpose of the Java memory model

     Define the access rules for each variable in the program , that is, the underlying details of storing and retrieving variables in and out of memory in the virtual machine.

     The above variables are slightly different from variables in Java programs: the variables include instance fields, static fields, and elements that make up an array object, excluding local variables and method parameters, because the latter are private to threads and will not be shared.

 

 2> Main memory and working memory

     ①Main memory:       

      The java memory model stipulates that all variables are stored in the main memory, where the main memory can be analogous to the hardware main memory, but the main memory here is part of the jvm.

     ②Working memory:

      "1" can be compared with the cache of the processor. The working memory of the thread saves the copy of the variables used by the thread in the main memory. All read and write operations of the thread to the variables must be performed in the working memory, not in the main memory. conduct;

      "2" Different threads cannot access variables in each other's working memory;

      "3" The transfer of variable values ​​between threads is done through the main memory;

 

 

3> Understanding main memory and working memory from different angles

 

(1) The main memory and working memory are not the same level of memory division as the java heap, stack, and method area in the java memory area;

(2) If the two division methods correspond: the main memory corresponds to the instance part in the heap, and the working memory corresponds to a part of the virtual machine stack;

(3) At a low level: the main memory is the hardware memory. To obtain a faster computing speed, the hardware system of the virtual machine may give priority to the working memory over the register and cache.

 

2. Interaction between memory

       The interaction protocol between the main memory and the working memory, that is, how a variable is copied from the working memory to the working memory, and the implementation details of how the variables in the working memory are synchronized back to the main memory.

       The operation in java memory model definition 8 is completed:

      (1) lock (lock): a variable acting on the main memory, representing a scalar as a thread-exclusive state;

      (2) unlock (unlock): a variable acting on the main memory to unlock a locked variable, and the unlocked variable can be locked by other threads;

      (3) read (read): a variable acting on the main memory, transferring the value of a variable from the main memory to the working memory of the thread for subsequent load operations;

      (4) load (load): acting on the working memory, putting the variable value read from the main memory by the read operation into the variable copy of the working memory;

      (5) use (use): acting on the working memory, passing a variable value in the working memory to the execution engine, and executing the operation whenever the virtual machine needs a bytecode instruction that uses the variable value;

      (6) assign (assignment): acting on the working memory, assigning a value received from the execution engine to the working memory variable;

      (7) store (storage): a variable acting on the working memory, passing a value in the working memory to the main memory for subsequent write operations;

      (8) write (write): acting on the main memory, putting the value of the variable obtained from the working memory of the store operation into the variable of the main memory

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326198403&siteId=291194637