Home  >  Article  >  Java  >  What does the Java memory model do?

What does the Java memory model do?

王林
王林forward
2023-05-09 09:01:211062browse

What is JMM

  • JMM’s full name is Java Memory Model, Chinese translation is Java Memory Model, a memory model that conforms to the memory model specification and shields the access differences of various hardware and operating systems. , ensuring that Java programs can ensure consistent mechanisms and specifications when accessing memory under various platforms.

  • #The Java memory model stipulates that all variables are stored in main memory, and each thread has its own working memory.

  • The working memory of a thread stores a main memory copy of the variables used in the thread. All operations on variables by the thread must be performed in the working memory. It cannot directly read and write main memory.

  • Different threads cannot directly access the variables in each other's working memory. The transfer of variables between threads requires data synchronization between their own working memory and main memory.

  • The JMM acts on the data synchronization process between working memory and main memory. He specifies how and when data synchronization is done.

What does the Java memory model do?

Main memory and working memory

  • Main memory and working memory can be simply compared to a computer The concepts of main memory and cache in the memory model. It is particularly important to note that the main memory and working memory are not at the same level of memory division as the Java heap, stack, method area, etc. in the JVM memory structure and cannot be directly compared.

  • If we must reluctantly correspond, from the definition of variables, main memory, and working memory, main memory mainly corresponds to the object instance data part in the Java heap. Working memory corresponds to part of the virtual machine stack.

What is the use of volatile keyword

  • Ensure data memory visibility

  • Visibility

    Initial variables are first stored in main memory;

    Thread operation variables need to be copied from main memory to thread local memory;

    Thread local work Memory is an abstract concept, including cache, store buffer (will be discussed later), registers, etc.

  • If thread A and thread B want to communicate, they must go through the following two steps:

    Thread A stores local memory A in The updated shared variables are flushed to main memory.

    Thread B goes to main memory to read the shared variables that thread A has updated before.

After one thread modifies a shared variable, other threads can see (perceive) the modification (change) of the variable

  • This is true for both ordinary variables and volatile variables

    • The difference is: the special rules of volatile ensure that the new value after the volatile variable value is modified is immediately synchronized to the main memory , volatile variables are refreshed from main memory immediately before each use, so volatile guarantees the visibility of operating variables between multiple threads, while ordinary variables cannot guarantee this.

  • In addition to the volatile keyword that can achieve visibility, synchronized, Lock, final (immutable) are also possible

    Use the synchronized keyword, at the beginning of the synchronization method/synchronization block (Monitor Enter), when using the shared variable, the variable value will be refreshed from the main memory to the working memory (that is, the latest value will be read from the main memory to the thread private working memory), at the end of the synchronization method/synchronization block (Monitor Exit), the variable values ​​​​in the working memory will be synchronized to the main memory (that is, the values ​​​​in the thread's private working memory will be written to the main memory for synchronization) .

    Use the most commonly used implementation of the Lock interface, ReentrantLock (reentrant lock), to achieve visibility: When we execute the lock.lock() method at the beginning of the method, this is the same as the synchronized starting position (Monitor Enter) They have the same semantics, that is, when using shared variables, the variable value will be refreshed from the main memory to the working memory (that is, the latest value will be read from the main memory to the thread's private working memory), and the lock will be executed in the final block of the method. The .unlock() method has the same semantics as the synchronized end position (Monitor Exit), that is, it will synchronize the variable values ​​​​in the working memory to the main memory (i.e., write the values ​​​​in the thread's private working memory to the main memory). Synchronize).

    The visibility of the final keyword refers to: the variable modified by final, once initialized in the constructor, and the "this" reference is not passed out in the constructor ("this" reference Escape is very dangerous, other threads are likely to access an object that is only "half initialized" through this reference), then other threads can see the value of the final variable.

The above is the detailed content of What does the Java memory model do?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete