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.
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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

Atom editor mac version download
The most popular open source editor