???????Java??????????????
???????? ???????[ 2017/4/5 14:21:45 ] ??????????????????? Java
??????????
????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??“????”?????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11