This is the 20th day of my participation in the August More Text Challenge

concept

The Java Memory Model (JMM) is an abstract concept that doesn’t really exist. It describes a set of rules or specifications that define how variables in a program (including instance fields, static fields, and elements that make up array objects) can be accessed.

  • Define access rules for various variables in a program
  • The low-level details of storing variable values in memory
  • Fetching the low-level details of variable values from memory

Why do WE need a JMM memory model

Shielding the memory access differences of various hardware and operating systems to achieve one

Two large memory

  • Main memory

    • The object instance data portion of the Java heap
    • Memory corresponding to physical hardware
  • The working memory

    • Part of the Java stack
    • Priority is given to registers and caches

Several specifications for the Java memory model

1. All variables are stored in the main memory

2. The main memory is part of the VM memory

3. Each thread has its own working memory

4. The thread’s working memory holds the main memory copy of the variable

5. Thread operations on variables must be performed in working memory

6. Different threads cannot directly access variables in each other’s working memory

7. The transfer of variable values between threads is done through main memory

Because the JVM to run the program of the entity is a thread, and every thread creation when the JVM to create a working memory (called the stack space) in some places, the working memory is the private data area each thread, while the Java memory model that all variables are stored in main memory, main memory is Shared memory region, all threads can access, But the thread to the operation of the variable (reading assignment, etc.) must be conducted in the working memory, the first thing to copy the variables from the main memory to their working memory space, and operation, the variable operation to complete before you write variables will be the main memory, cannot be directly operating variables in main memory, working memory storage in various threads of variables in the main memory copy copy, Therefore, different threads cannot access each other’s working memory, and communication (passing values) between threads must be done through main memory

The Java memory model has three major features

  • Visibility (When one thread changes the value of a shared variable, other threads are immediately aware of the change)
  • Atomicity (an operation or series of operations that are indivisible and either succeed or fail at the same time)
  • Orderliness (the order of variable assignment operations is the same as the order of execution in the program code)