??????????
????Java??????????????:

???????е????????????????????????????????????????????????????ж?????????????????????????????????????????????????????????????????????????????????
????????????????
public class Singleton {
private static Singleton instance;
private Singleton(){
}
public static Singleton getInstance() {
if (instance == null) //1??A??????
instance = new Singleton(); //2??B??????
return instance;
}
}
???????????
public class SafeLazyInitialization {
private static Singleton instance;
public synchronized static Singleton getInstance() {
if (instance == null)
instance = new Singleton();
return instance;
}
}
????????????????????????????????
??????????????
public class Singleton{
private static Singleton singleton;
private Singleton(){
}
public static Singleton getInstance(){
if(null == singleton){  //????μ??
synchronized(Singleton.class){  //????
if(null == singleton){  //????μ??
singleton = new Singleton();//???????????????
}
}
}
return singleton;
}
}
???????????????????????????????????????????????????????????к????????????????????????????????Щ???β????????????????????????????JVM?????μ??????????????????????衣
????· ???????
????· ???????????
????· ??????????????????
??????????????????????????????????????????????????????????????????
???????????
?????????????JVM?????????Ч??????????????????????????????????????????????????????е????????????п???(??????п???)????2??3?????????????????????????????????????
????java????????????е????????:

??????????????????????????instance = new Singleton();?????????????????д????????????μ?????α????
????memory = allocate();   //1???????????????
????ctorInstance(memory);  //2???????????
????instance = memory;     //3??????instance?????????????
????????????α?????е?2??3?????????????????ЩJIT?????????????????????????????????????ο?????1??“Out-of-order writes”???????2??3??????????????????????£?
????memory = allocate();   //1???????????????
????instance = memory;     //3??????instance?????????????
????//????????????б????????
????ctorInstance(memory);  //2???????????

??????????????е????????:

???????????
????????Volatile????????
??????????Volatile???????????壺
????· ??????????????????
????· ????????????????????? synchronized ???н????
????· ?????????????????????????JVM?淶??
????Volatile ??????????
public class Singleton{
private volatile static Singleton singleton;
private Singleton(){
}
public static Singleton getInstance(){
if(null == singleton){
synchronized(Singleton.class){
if(null == singleton){
singleton = new Singleton();
}
}
}
return singleton;
}
}
??????????????????????
???????t??????????????????????????????JVM???????γ?????????????????????????????????????????????е???????????????????????????JVM???????????????????????????????????????????Σ?????singleton???????????

???????????????????????“???????”??????α?????е?2??3??????????????????????????????B??“????”?????????