Volatile的内存语义

1、volatile的特性

可见性:对一个volatile变量的读,总能够看到任意一个线程对这个volatile变量的写入。

原子性:对任意单个volatile变量的读/写具有原子性,但类似于volatile++这种复合操作不具有原子性。

接下来我们用程序验证。


public class OldVolatileFeaturesExample {

    volatile long v1 = 0L;      // 使用volatile 声明64位的long型变量
    //long v1 = 0L;

    public void set(long l){
        v1 = l;                 //单个volatile 变量的写
    }

    public void getAndIncrement(){
        v1++;                   // 多个volatile 变量的读/写
    }

    public long get(){
        return v1;              //  单个volatile 变量的读
    }

    public static void main(String[] args) {

        final OldVolatileFeaturesExample volatileFeaturesExamlple = new OldVolatileFeaturesExample();

        Thread thread0 = new Thread(new Runnable() {
            public void run() {
                volatileFeaturesExamlple.set(1L);
            }
        });
        thread0.start();

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                volatileFeaturesExamlple.getAndIncrement();
            }
        });
        thread1.start();

        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                long l = volatileFeaturesExamlple.get();
                System.out.println("创建的l值-------"+ l);
            }
        });
        thread2.start();

/*        for (int i = 0; i < 10; i++) {

            Thread thread0 = new Thread(new Runnable() {
                public void run() {
                    volatileFeaturesExamlple.set(1L);
                }
            });
            thread0.start();

            Thread thread1 = new Thread(new Runnable() {
                public void run() {
                    volatileFeaturesExamlple.getAndIncrement();
                }
            });
            thread1.start();

            Thread thread2 = new Thread(new Runnable() {
                public void run() {
                    long l = volatileFeaturesExamlple.get();
                    System.out.println("创建的l值-------"+ l);
                }
            });
            thread2.start();
        }*/


    }

}

这里,线程thread0 设置使用volatile修饰的long类型变量 v1 ;线程thread1 进行v1++操作, thread2 获取变量v1的值,并打印结果。那么 v1的值是几呢?

上面这段程序运行结果是:

创建的l值-------2

那么就算不用volatile修饰的v1变量,也执行上述操作,结果会是什么样子呢?没错,还是2。

那么使用volatile修饰的v1变量 当使用for 循环呢?也就是多个volatile变量的读写操作的结果:


创建的l值-------2
创建的l值-------2
创建的l值-------1
创建的l值-------1
创建的l值-------1
创建的l值-------2
创建的l值-------3
创建的l值-------1
创建的l值-------2
创建的l值-------3

假设具有原子性,那么v1循环加10次 1,那么它的结果应该是 10,而不是上面的结果。上面的程序等价于:



public class NewVolatileFeaturesExample {

    long v1 = 0L;

    public synchronized void set(long l){  //对单个的普通变量的写用同一个锁同步
        v1 = l;
    }

    public void getAndIncrement(){         //普通方法调用
        long temp = get();                 //调用已同步的读方法
        temp += 1L;                        //普通写操作
        set(temp);                         //调用已同步的写方法
    }

    public synchronized long get(){         // 对单个的普通变量的读用同一个锁同步
        return v1;
    }

    public static void main(String[] args) {
        final NewVolatileFeaturesExample newVolatileFeaturesExample = new NewVolatileFeaturesExample();

/*        for (int i = 0; i < 10; i++) {

            Thread thread0 = new Thread(new Runnable() {
                public void run() {
                    newVolatileFeaturesExample.set(1L);
                }
            });
            thread0.start();

            Thread thread1 = new Thread(new Runnable() {
                public void run() {
                    newVolatileFeaturesExample.getAndIncrement();
                }
            });
            thread1.start();

            Thread thread2 = new Thread(new Runnable() {
                public void run() {
                    long l = newVolatileFeaturesExample.get();
                    System.out.println("创建的l值-------"+ l);
                }
            });
            thread2.start();
        }*/

        Thread thread0 = new Thread(new Runnable() {
            public void run() {
                newVolatileFeaturesExample.set(1L);
            }
        });
        thread0.start();

        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                newVolatileFeaturesExample.getAndIncrement();
            }
        });
        thread1.start();

        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                long l = newVolatileFeaturesExample.get();
                System.out.println("创建的l值-------"+ l);
            }
        });
        thread2.start();
    }
}

这个也就是相当于对v1变量的读和写进行了synchronized 同步锁操作。

而锁的语义决定了临界区代码的执行具有原子性。锁的happens-before 规则保证了释放锁和获取锁的两个线程之间的内存可见性。那么volatile 写和读建立的happens-before 关系是又是什么样子呢?欲知后事如何,请看下回分解。

更多创作在我的公众号里哦。
在这里插入图片描述

相关推荐

  1. c语言volatile

    2024-05-25 23:41:10       51 阅读
  2. 嵌入式C语言关键字volatile

    2024-05-25 23:41:10       56 阅读
  3. C语言volatile关键字用法

    2024-05-25 23:41:10       43 阅读
  4. volatile作用

    2024-05-25 23:41:10       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-25 23:41:10       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-25 23:41:10       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-25 23:41:10       82 阅读
  4. Python语言-面向对象

    2024-05-25 23:41:10       91 阅读

热门阅读

  1. MySQL和MongoDB数据库的区别

    2024-05-25 23:41:10       32 阅读
  2. Python——字典数据存入excel

    2024-05-25 23:41:10       36 阅读
  3. 考研英语二小作文专项练习题目和简单示例

    2024-05-25 23:41:10       35 阅读
  4. H.264 的RTP 三种封包模式

    2024-05-25 23:41:10       38 阅读
  5. docker迁移根目录导致的权限问题

    2024-05-25 23:41:10       35 阅读
  6. docker 安装RabbitMQ

    2024-05-25 23:41:10       38 阅读
  7. SQL的多表联查

    2024-05-25 23:41:10       37 阅读