一个解释volatile关键字作用的最好的例子

闲话少叙,上代码

package com.dwz.concurrency2.chapter3;

public class VolatileTest {
    private static volatile int INIT_VALUE = 0; 
    private static final int MAX_LIMIT = 5;
    
    public static void main(String[] args) {
        new Thread(() -> {
       //jvm自作多情的做了一个优化,当对一个变量只是读操作的时候,只是访问缓存的数据,只有进行写操作的时候,会访问主内存的数据
int localValue = INIT_VALUE; while (localValue < MAX_LIMIT) { if(localValue != INIT_VALUE) { System.out.printf("The value updated to [%d] ", INIT_VALUE); localValue = INIT_VALUE; } } }, "READER").start(); new Thread(() -> { int localValue = INIT_VALUE; while (localValue < MAX_LIMIT) { System.out.printf("update the value to [%d] ", ++localValue); INIT_VALUE = localValue; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } }, "UPDATER").start(); } }

测试结果,出现两种情况:

1.INIT_VALUE被volatile关键字修饰时,READER线程是可以感知到UPDATER的变化

update the value to [1]
The value updated to [1]
update the value to [2]
The value updated to [2]
update the value to [3]
The value updated to [3]
update the value to [4]
The value updated to [4]
update the value to [5]
The value updated to [5]

2.INIT_VALUE缺少volatile关键字修饰时,READER线程感知不到UPDATER的变化

update the value to [1]
update the value to [2]
update the value to [3]
update the value to [4]
update the value to [5]

 volatile可以保证内存可见性,有序性,不让jvm自作主张去优化,可以保证读之前的写操作完成,但是不能保证原子性

package com.dwz.concurrency2.chapter3;

public class VolatileTest2 {
    private static volatile int INIT_VALUE = 0; 
    private static final int MAX_LIMIT = 500;
    
    public static void main(String[] args) {
        new Thread(() -> {
            while (INIT_VALUE < MAX_LIMIT) {
                System.out.println("T1->" + (++INIT_VALUE));
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "ADDER-1").start();
        
        new Thread(() -> {
            while (INIT_VALUE < MAX_LIMIT) {
                System.out.println("T2->" + (++INIT_VALUE));
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "ADDER-2").start();
    }
}

测试结果:ADDER-1中的INIT_VALUE 和 ADDER-2中的INIT_VALUE会出现重复数据,因此volatile关键字是不能保证原子性的

volatile的使用场景
1.状态量标记
volatile boolean start = true;
while(start) {

}

void close() {
start = false;
}
2.屏障前后的一致性

volatile关键字在多线程三个特性角度:
一旦一个共享变量被volatile修饰,具备以下含义:
1.保证了不同线程间的可见性
2.禁止对其进行重排序,也就是保证了有序性
3.并未保证原子性


volatile关键字实质:
1.保证重排序的时候不会把后面的指令放到屏障的前面,也不会把前面的放到后面
2.强制对缓存的修改操作会立刻写入主存
3.如果是写操作,它会导致其他CPU中的缓存失效

原文地址:https://www.cnblogs.com/zheaven/p/12100231.html