Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java memory Model

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand the Java memory model", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the Java memory model.

(1) Overview

Many people confuse the Java memory area (runtime data area) with the Java memory model (JMM), which are completely different things.

Java memory region refers to the regional storage of JVM runtime data, while the Java memory model defines the abstract relationship between thread and main memory. Understanding the Java memory model is the basis of learning Java concurrent programming.

(2) Java memory model

The Java memory model stipulates that all variables are stored in the main memory, and each thread has its own working memory. All operations on the variables by the thread must be carried out in the working memory, instead of directly reading and writing the variables in the main memory. Let's look at a picture:

Each thread has its own private working memory, copy a copy from the main memory to the working memory when the variable is needed, and refresh the shared variable to the main memory if the variable is updated.

But between two threads, there is no way to read the value of variables in each other's working memory. Look at an example:

Public class Test {private static boolean flag=false; public static void main (String [] args) throws InterruptedException {new Thread (new Runnable () {@ Override public void run () {System.out.println ("waiting"); while (! flag) {} System.out.println ("in");}) .start () Thread.sleep (2000); new Thread (new Runnable () {@ Override public void run () {System.out.println ("change flag"); flag=true; System.out.println ("change success");}}) .start ();}}

First, a static variable flag is defined as false,A thread waiting for flag equals true to output in, so we open a new thread to modify flag to true. The result is that thread An is still unable to output in.

If you look at the principle of the Java memory model, you can understand that different threads modify variables that are not visible to local threads.

(3) Atomic operation of JMM data

From the above code, we already know the structure of the Java memory model, so how to read variables between working memory and main memory and how to modify variables? JMM provides a series of atomic operations on variables. Let's put aside the theory and take a look at a diagram that describes the execution of the above code:

There are ten steps in the whole process. I won't repeat a few steps. I'll make a list of six operations that are not repeated:

Read: reading data from main memory

Load: writes data read from main memory to working memory

Use: read data from working memory to use

Assign: re-assign calculated values to working memory

Store: writes working memory data to main memory

Write: assigns past variables of store to variables in main memory

Through the above diagram, we should be able to have a deeper understanding of the following six atomic operations. There are eight atomic operations in JMM. The remaining two are listed below.

Lock: locks the main memory variable and identifies it as thread exclusive state

Unlock: unlock the main memory variable, and other threads can lock it after unlocking it

(4) JMM cache inconsistency

As we have seen from the previous example, one thread modifies the data and the other thread is not immediately visible. This is the problem of JMM cache inconsistency. There are two solutions:

Lock:

Remember that we have not used the last two JMM operations, lock and unlock, we can use these two operations to achieve cache consistency, when a thread wants to get a main memory variable, first use lock to lock the main memory variable, only he can use it, and then unlock after using it, other threads can compete. But locking means low performance.

MESI Cache consistency Protocol:

This protocol involves the bus sniffing mechanism of cpu. From the flow chart of JMM execution above, we can see that when a thread modifies the shared variable, it will write back to the main memory. MESI cache consistency protocol invalidates the data of other threads that are also using this variable through cpu's bus sniffing mechanism, so that these threads have to re-read the values in the main memory, thus ensuring the final consistency of the cache.

At this point, I believe you have a deeper understanding of "how to understand the Java memory model". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report