可見性關鍵字(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
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
結果分析:
volidate就是保證每次讀數據都會從內存中讀取,但只是保證多線程內共享資源的可見性。
可見性,只是保證取出來的數據是當前內存中放的數據,但無法保證數據一定寫入正確。