synchronized關(guān)鍵字是防止多個(gè)線程同時(shí)執(zhí)行一段代碼,那么就會(huì)很影響程序執(zhí)行效率,而volatile關(guān)鍵字在某些情況下性能要優(yōu)于synchronized,但是要注意volatile關(guān)鍵字是無(wú)法替代synchronized關(guān)鍵字的,因?yàn)関olatile關(guān)鍵字無(wú)法保證操作的原子性。通常來(lái)說(shuō),使用volatile必須具備以下2個(gè)條件:
1、對(duì)變量的寫操作不依賴于當(dāng)前值。
2、該變量沒(méi)有包含在具有其他變量的不變式中。
實(shí)際上,這些條件表明,可以被寫入 volatile 變量的這些有效值獨(dú)立于任何程序的狀態(tài),包括變量的當(dāng)前狀態(tài)。
事實(shí)上,我的理解就是上面的2個(gè)條件需要保證操作是原子性操作,才能保證使用volatile關(guān)鍵字的程序在并發(fā)時(shí)能夠正確執(zhí)行。
下面列舉幾個(gè)Java中使用volatile的幾個(gè)場(chǎng)景。
● 狀態(tài)標(biāo)記量
volatile boolean flag = false;
while(!flag){
doSomething();
}
public void setFlag() {
flag = true;
}
volatile boolean inited = false;
//線程1:
context = loadContext();
inited = true;
//線程2:
while(!inited ){
sleep()
}
doSomethingwithconfig(context);
● double check
class Singleton{
private volatile static Singleton instance = null;
private Singleton() {
}
public static Singleton getInstance() {
if(instance==null) {
synchronized (Singleton.class) {
if(instance==null)
instance = new Singleton();
}
}
return instance;
}
}