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

Case Analysis of volatile and JMM multithreaded memory Model for Java concurrent programming

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "volatile and JMM multithreaded memory model case analysis of Java concurrent programming". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "Java concurrent programming of volatile and JMM multithreaded memory model case analysis"!

First, look at the phenomenon through the program

Before we start to explain the Java multithreaded caching model, let's take a look at the following code. The logic of this code is simple: the main thread starts two child threads, one thread 1 and one thread 2. Thread 1 executes first, and thread 2 executes after sleep sleeps for 2 seconds. The two threads use a shared variable shareFlag with an initial value of false. If shareFlag is always equal to false, thread 1 will always be in a dead loop, so we set shareFlag to true in thread 2.

Public class VolatileTest {public static boolean shareFlag= false; public static void main (String [] args) throws InterruptedException {new Thread (()-> {System.out.print ("start thread 1 = >"); while (! shareFlag) {/ / shareFlag= false is endless loop / / System.out.println ("shareFlag=" + shareFlag);} System.out.print ("thread 1 execution completion = >");} start () Thread.sleep (2000); new Thread (()-> {System.out.print ("start execution thread 2 = >"); shareFlag = true; System.out.print ("thread 2 execution completion = >");}) .start ();}}

If you haven't learned the JMM threading model, you might want the output from the above code to look something like this:

Start execution thread 1 = > start execution thread 2 = > thread 2 execution completion = > thread 1 execution completion = >

As shown in the figure below, normal people understand this code and first execute thread 1 into the loop, thread 2 modifies the shareFlag=true, and thread 1 jumps out of the loop. So thread 1 that jumps out of the loop will print "Thread 1 execution completion = >", but after the author's experiment, * * "Thread 1 execution completion = >" will not be printed, and thread 1 will not jump out of the endless loop * *. Why?

Second, why does this phenomenon occur (JMM model)?

To explain the problems mentioned above, we need to learn the JMM (Java Memory Model) Java memory model. I think it is more accurate to call it the Java multithreaded memory model.

First of all, each thread in JMM has its own working memory. When the program starts, the thread loads (read&load) the shared variable into its own working memory, and the memory variable loaded into the thread's working memory is a copy of the shared variable in the main memory. That is to say, there are three copies of shareFlag in memory at this time, and the values are all equal to false.

Thread 2 modifies its working memory copy to shareFlag=true when it executes shareFlag=true, and synchronously writes back (store&write) the value of the copy to main memory.

But the shareFlag=false in thread 1's working memory has not changed, so thread 1 has been in an endless loop.

3. MESI cache consistency protocol

According to the above experiment and the JMM model, thread 1 is not aware of the value of the shared variable modified by thread 2. So how do you make thread 1 aware of a change in the value of a shared variable? In fact, it is also very simple, just add the volatile keyword to the shareFlag shared variable.

Public volatile static boolean shareFlag = false

The underlying principle is like this, plus the volatile keyword prompts JMM to follow the MESI cache consistency protocol, which contains the following cache usage specifications (if you don't understand it, you don't have to read it, which will be described in simple language and examples below).

Modified: the data that represents the current Cache row is Dirty and is modified only in the Cache of the current CPU; at this point, the data in this Cache row is different from that in other Cache and different from that in memory.

Exclusive: the data representing the current Cache row is valid data, which is not available in the Cache of other CPU; and the current Cache row data is the same as the data in memory.

Shared: this line of data is cached in the Cache representing multiple CPU, and the data in Cache is consistent with the data in memory

Invalid: indicates that the data in the current Cache row is invalid

The cache usage specification above may be too complex, to put it simply

When thread 2 modifies the shareFlag (see Modify), tell the bus bus that I modified the shared variable shareFlag

Thread 1 listens on the Bus bus, and when it learns that the shared variable shareFlag has been modified, it deletes the copy of shareFlag in its working memory to invalidate it.

When thread 1 needs to use shareFlag again and finds no copy of the shareFlag variable in working memory, it will reload (read&load) from main memory.

At this point, I believe you have a deeper understanding of "volatile and JMM multithreaded memory model instance analysis of Java concurrent programming". 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