current position:Home>Java Concurrent Programming atlas learning (VI) interview volatile keyword

Java Concurrent Programming atlas learning (VI) interview volatile keyword

2022-05-15 05:18:41Peter Pan 1231

To a volatile Domain writing , happens-before On Any follow-up on this volatile Domain reading

One 、happens-before And volatile Variable rule

JMM Customized for the compiler volatile The rules of reordering , that JMM How can reordering be prohibited ? The answer is Memory barrier  

Two 、 Memory barrier (Memory Barriers / Fences)

In order to achieve volatile Memory semantics of , When the compiler generates bytecode , Will insert... In the instruction sequence Memory barrier To disable certain types of processor reordering

This sentence is a little abstract , Try to imagine The memory barrier is a high wall , If there's a barrier between two variables , Then they can't swap places ( Reorder ) 了 , Variables are read (Load) Yes (Store), Before and after operation , JMM The memory barrier insertion strategy is divided into 4 Kind of :

1. At every volatile Write Operation of the front Face inserts a StoreStore barrier

2. At every volatile Write Operation of the after Face inserts a StoreLoad barrier

3. At every volatile read Operation of the after Face inserts a LoadLoad barrier

4. At every volatile read Operation of the after Face inserts a LoadStore barrier

 

3、 ... and 、volatile Write - Memory semantics of reading

  Assume threads A Execute first writer Method , Subsequent threads B perform reader Method

public class ReorderExample {

    private int x = 0;
    private int y = 1;
    private volatile boolean flag = false;

    public void writer(){ 
        x = 42; //1
        y = 50; //2
        flag = true; //3
    }

    public void reader(){ 
        if (flag){ //4
            System.out.println("x:" + x); //5
            System.out.println("y:" + y); //6 
        }
    } 
}

volatile Memory semantics of reading :

 

When reading a volatile variable , JMM This thread will correspond to The local memory setting is invalid . The thread will then read the shared variables from main memory .

So when threads B perform reader When the method is used , Threads B Invalid local memory variable , Read variables from main memory to local memory , And you get the thread A The result of the change , This is it. volatile How to ensure that ⻅ Sexual

  1. Threads A Write a volatile Variable , It's essentially a thread A To read this next volatile Variable One of the threads of the sent out ( The ) news

  2. Threads B Read a volatile Variable , It's essentially a thread B Received from a previous thread ( Writing This volatile Changes made to shared variables before ) news .

  3. Threads A Write a volatile Variable , Subsequent threads B Read this volatile Variable , The essence of the process is There are threads on it A From main memory to thread B Send a message .

 

copyright notice
author[Peter Pan 1231],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/131/202205111242178903.html

Random recommended