Java

    技术2026-02-01  8

    volatile 学习笔记2:

    定义静态变量x, y 初始都为0,

    开启两个线程,分别把x,y 的值赋值给局部变量 a,b , 并且同时更新x, y 的值, 并把a, b的值保存起来。

    会发现两个线程的执行顺序不定,并且同一个线程的两条语句执行的顺序也不一定,导致a,b的值共有4种情况。

    其原因是CPU 对指令会重排, 解决方法是给x, y 变量用volatile 修饰,则CPU 不会再进行对它操作的指令重排

    public static int x = 0, y = 0; public static void main(String args[]) throws InterruptedException { test2(); } private static void test2() throws InterruptedException { Set<String> resultSet = new HashSet<>(); final Map<String, Integer> resultMap = new HashMap<>(); for (int i = 0; i < 100000; i++) { resultMap.clear(); x = y = 0; Thread thread1 = new Thread(new Runnable() { @Override public void run() { int a = y; x = 1; resultMap.put("a", a); } }); Thread thread2 = new Thread(new Runnable() { @Override public void run() { int b = x; y = 1; resultMap.put("b", b); } }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); resultSet.add("a=" + resultMap.get("a") + ", " + "b=" + resultMap.get("b")); System.out.println(resultSet); } } [a=1, b=1, a=1, b=0, a=0, b=1, a=0, b=0] [a=1, b=1, a=1, b=0, a=0, b=1, a=0, b=0] Process finished with exit code 0

    可见其结果 a=1, b=1 这种情况比较少见。

    添加Volatile对x, y修饰后

    public volatile static int x = 0, y = 0;

    结果:

    [a=1, b=0, a=0, b=1, a=0, b=0] [a=1, b=0, a=0, b=1, a=0, b=0] Process finished with exit code 0

    相应的,Kotlin 上使用关键字 internal 修饰即可

    Processed: 0.010, SQL: 9