This is the 18th day of my participation in the August Challenge

Java memory model

Next, let’s go to the Java memory model. Our learning objectives are twofold: to learn the concepts of the Java memory model and its functions.

Let’s take a look at the concept of Java Memory Model, which is called Java Memory Model. We usually call it JMM for short.

The Java memory model is now a hot topic in interviews, and it is worth noting that the Java memory model should not be confused with the JJava memory structure. The JVM divides memory into stacks, method areas, etc. This refers to the Java memory structure.

The Java Memory Model is a set of specifications. The definitive explanation of the Java memory model can be found at download.oracle.com/otn-pub/jcp… . Java memory model (volatile, volatile, volatile, volatile, volatile, volatile, volatile, volatile, volatile)

Warm tip: interested can read this document, for everyone is very helpful.

Why the Java memory model? As we know, Java is a cross-platform language, which can run on different operating systems. The underlying layer relies on THE JVM virtual machine to carry out cross-platform. Each platform has a corresponding VIRTUAL machine, so our Java programs can run on different operating systems.

The Java Memory model is a part of the Java Virtual Machine specification that hides the details of how our Java programs run on different operating systems. Let our Java programs just focus on the Java memory model. The Java Memory model is a set of specifications that describe the access rules for the various variables we add, mainly thread-shared variables. It mainly describes the low-level details of variable storage and reading in Java.

The Java memory model divides memory into two parts, main memory and working memory.

First, let’s look at main memory, where we have shared variables in Java, such as class member variables, which we also call instance variables. And static member variables, or class variables, are stored in main memory, which can be accessed by any thread.

Then we look at the second part of working memory. Each thread has its own working memory, and when our thread wants to execute code, it has to process it in working memory. For example, if a thread accesses a shared variable X and wants to operate on it, the thread cannot directly operate on the shared variable in main memory. What happens? It must first make a copy of the shared variable into its working memory, then process the data, and then synchronize it back to the main memory.

Let’s say thread 1 wants to operate on a shared variable, so there’s a shared variable int X = 10, so the first thing this thread has to do is make a copy of the shared variable, put it in its working memory, and process it. Let’s say that after it’s done, it wants to synchronize the result back to main memory, and then other threads do the same, copying the shared variable back to their working memory, doing the same, and then synchronizing it back again.

So let’s go over here and take a closer look at main memory, which is accessible to data shared by all threads. The main idea is to include member variables in a class, including static adoption variables. Note that thread local variables are no longer available in main memory, because thread local variables are not available only to the thread itself. Working memory means that each thread has working memory, and the working memory stores a copy of the shared variable. All thread operations on the shared variable are performed on the copy first, and then synchronized back to the main memory.

Next, let’s look at the Java memory model in action. The Java memory model is a set of specifications whose main purpose is to ensure the visibility, order, and atomicity of shared variables when multithreading reads and writes them. The Java memory model specification produced two key words: synchronized and volatile. We also use these two keywords to ensure these three features of the program.

What does the Java memory model have to do with the memory structure of a real computer?

First, on the right, this is the hardware architecture of our real computer, including the CPU, which has registers, caches, and memory. And then on the left is our Java memory model. Know that the Java Memory model is an abstract set of specifications, an abstract set of concepts. Working memory in the Java memory model, which may correspond to the CPU register, may correspond to the CPU cache, may correspond to the memory. The main memory in the Java memory model may correspond to a CPU register, or it may correspond to a cache.

Summary: At this point, we have learned the concepts and functions of the Java memory model, let’s summarize, we know that the Java memory model is a set of specifications. The main goal is to make our Java programs cross-platform without having to pay attention to the underlying details of the platform. The Java memory model describes the access rules of various variables. The Java memory model can ensure that we solve the problems of visibility, order and atomicity when multi-threading operations on shared data.