6.可见性关键字(volidate)
可见性关键字(volidate): 如果对java内存模型了解较清楚的话,我们知道每个线程都会被分配一个线程栈。
线程栈里存的是对象的引用,但当前cache缓存机制,可能会把数据拷贝。
就是,命中缓存,去数据是从cache中获取,而不是从本地内存读取。
不加关键字实例:
package com.xm.thread.t_19_01_27;
import java.util.concurrent.TimeUnit;
public class VolatileDemo implements Runnable{Boolean state = false;
volatile int count = 0;
@Override
public void run() {
if(state==true || count<10) {
count ++;
System.out.println("state="+state+";
count="+count);
} else {
System.out.println("state="+state+";
count="+count);
}
}public static void main(String[] args) throws InterruptedException {
VolatileDemo demo = new VolatileDemo();
for(int i=0;
i<100;
i++) {
new Thread(demo).start();
}TimeUnit.MILLISECONDS.sleep(10);
demo.state = true;
TimeUnit.MILLISECONDS.sleep(10);
demo.state = false;
TimeUnit.MILLISECONDS.sleep(10);
demo.state = true;
}
}
运行结果:
state=false; count=2加关键字实例:
state=false; count=2
state=false; count=3
state=false; count=2
state=false; count=4
state=false; count=5
state=false; count=6
state=false; count=7
state=false; count=8
state=false; count=9
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=true; count=19
state=true; count=18
state=true; count=17
state=true; count=16
state=true; count=15
state=true; count=15
state=true; count=15
state=true; count=13
state=true; count=12
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
package com.xm.thread.t_19_01_27;
import java.util.concurrent.TimeUnit;
public class VolatileDemo implements Runnable{volatile Boolean state = false;
volatile int count = 0;
@Override
public void run() {
if(state==true || count<10) {
count ++;
System.out.println("state="+state+";
count="+count);
} else {
System.out.println("state="+state+";
count="+count);
}
}public static void main(String[] args) throws InterruptedException {
VolatileDemo demo = new VolatileDemo();
for(int i=0;
i<100;
i++) {
new Thread(demo).start();
}TimeUnit.MILLISECONDS.sleep(10);
demo.state = true;
TimeUnit.MILLISECONDS.sleep(10);
demo.state = false;
}
}
运行结果:
state=false; count=1结果分析: volidate就是保证每次读数据都会从内存中读取,但只是保证多线程内共享资源的可见性。
state=false; count=2
state=false; count=3
state=false; count=4
state=false; count=5
state=false; count=6
state=false; count=7
state=false; count=8
state=false; count=9
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=true; count=12
state=false; count=10
state=true; count=14
state=true; count=17
state=true; count=18
state=true; count=21
state=true; count=25
state=true; count=28
state=false; count=10
state=true; count=31
state=true; count=32
state=true; count=33
state=true; count=35
state=true; count=29
state=true; count=28
state=true; count=26
state=true; count=24
state=true; count=23
state=true; count=22
state=true; count=20
state=true; count=19
state=true; count=17
state=true; count=15
state=true; count=13
state=true; count=11
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=false; count=10
state=true; count=42
state=true; count=41
state=true; count=41
state=true; count=40
state=true; count=39
state=true; count=39
state=true; count=39
state=true; count=34
state=true; count=31
可见性,只是保证取出来的数据是当前内存中放的数据,但无法保证数据一定写入正确。
【6.可见性关键字(volidate)】转载于:https://www.cnblogs.com/TimerHotel/p/thread06.html
推荐阅读
- 第6.2章(设置属性)
- 五年后,我要成为独立自强自信的女性
- 也许,是我记性不好,也许是社会太吵,内心太躁。
- 人性的弱点-笔记
- 从战略性的角度可以配置股票
- 同学,你可知道生涯规划的重要性()
- 【38】“劳逸结合”的重要性
- 西安临潼区夏令营性价比高
- 越努力越幸福
- Java基础-高级特性-枚举实现状态机