[Java] volatile Comments

Today to introduce volatitle

What is volatile

volatitle is a key to ensure that shared variables can be accurately and consistently updated (to ensure visibility), can only be used for variables

How volatile is to ensure visibility

When the shared variables volatile modifier modified write assembly code back more than a lock prefix instructions. The instruction has the following two functions:

  1. The data in the current cache line is written back to memory
  2. The other cpu caches data in the memory address is invalid (cache coherency mechanism)

The following chart to continue through memory model analysis

Memory model

How to prevent the volatile instruction reordering

First to introduce several types of memory barrier

Barrier type Explanation
LoadLoad large column  [java] volatile Detailed Barriers Ensure that data is loaded prior to Load1 and Load2 all subsequent load instruction
StoreStore Barriers Store1 ensure data is visible to other processors (refresh memory) is stored in the first and all subsequent Store2 store instruction
LoadStore Barriers Ensure that data is loaded prior to Store2 Load1 and all subsequent store instruction to the refresh memory
StoreLoad Barriers Loading data to ensure Store1 visible to other processors before Load2 and all subsequent load instruction

The following continue to analyze how volatile is to prevent instruction reordering

  1. In each of the foregoing volatile write operation to insert a barrier StoreStore
  2. Write back operation is inserted in each of a volatile barrier StoreLoad
  3. Inserting a volatile LoadLoad barrier after each read operation
  4. Inserting a volatile LoadStore barrier after each read operation

1.jpg

2.jpg

Today's introduction to end here, Everyone is invited to ask questions

My eyes only the vast expanse of stars, even if it out of reach

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11711201.html