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 关系是又是什么样子呢?欲知后事如何,请看下回分解。
更多创作在我的公众号里哦。