????д?????
????д???????????????????ддif else??????????????coding????????????????????????????????????????????????????????????????С???????е???coding??????????????????????????????????????????????д?????????????????д???????????????????????????б?????????????????????????к?????????????????????????????????????????ù??????????????????????????????????????????????????????Щ??????????????????????????????????????????????
???????弰???
????????????????????????????????????????????????????£?


?????????????????????????????????????????????????????
??????1?? ?????е??????
??????2?? ????????????????е???????
??????3?? ?????????????????е???????
???????????????????????????α????
public class Singleton{
private Singleton(){} //??е??????
private static Singleton singleton; //?????????????о??????
public static Singleton getInstance(){ //??????????????о??????
…… //???????????????????????????о??????
return singleton;
}
}
????????????????????????????????????????????????????л??????????????????
//readResolve method to preserve Singleton property
private Object readResolve() throws ObjectStreamException {
return INSTANCE;
}
//?????????ú????μ?Singleton???
private static boolean flag = false;
private Singleton(){
synchronized(Singleton.class)
{
if(flag == false){
flag = !flag;
} else{
throw new RuntimeException("?????????????");
}
}
}
//???????????μ????
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Cannot clone instance of this class");
}
?????????????????????????????????????????ó???????????????????????????????????????????
????√ ?????? —— ?????????????????????
????√ ?????? —— ??????????????·??????????????????
????√ ?????? —— ???????????????????????????????
????????????
????????????????????????????????????????????Щ???????????????????????б???????????????????????????????????????????????3???????????
?????? ??????????????????????
?????? ??????????????????????????????????????????
?????? ??????????????
?????? ?????????????????????
?????? ?????????????????????????????????
?????????
???????????????????????????????????????????????????????ж????????????????????????????????????????????????????????????????????????????????????????????
?????????????
public class Singleton {
private static Singleton instance=new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return instance;
}
}
??????????????????????????????????????????JVM?????????????????????????????????????磬?????????????????Lazy Loading???????????????????????????????б??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????б?????????????????????????????????????????????????????
??????????????????????????????д?????????????????????JVM????????????????????????????£?
????private static Singleton instance = null;
????static {
????instance = new Singleton();
????}
?????????????
?????????????????????????????????????????????????????α??????????????????????????????????????????????????????????????о?????????????????????????????????????????????£?
public class Singleton {
private static Singleton instance=null;
private Singleton() {}
public static Singleton getInstance(){
if(instance==null) {
instance=new Singleton();
}
return instance;
}
}
??????????????????????в?????????????????????????????ж??????????getInstance()???????Singleton?????????п?????????????????????п????????????getInstance()???????????????Singleton????????????????????????????????????????????????
public class Singleton {
private static Singleton instance=null;
private Singleton() {};
public static synchronized Singleton getInstance(){
if(instance==null){
instance=new Singleton();
}
return instance;
}
}
???????????????getInstance()?????????????????????????????????????????????????????Ч????μ????????????????getInstance()??????????????????????????????????????????????????????????????????????????????????????return????????????????????????????????????????????????н??????Ч??????????(Double Checked Locking)????
public class Singleton {
private static volatile Singleton instance=null;
private Singleton() {}
public static Singleton getInstance(){
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
???????????????????????????????????????????????????????
????1???????????????
?????????????getInstance()???????????μ?? instance == null???????????????????????????????????????????????????Σ??????????ж??????????????????? if??????????????????ж??μ??????????????????
????2?????????instance???volatile??????к?????
???????????instance = new Singleton()????????????????????JVM????仰??????????? 3 ?????飺
?????? ?? instance ???????
?????? ???? Singleton ?????????????????????
?????? ??instance??????????????????????? instance ??? null ???
??????????JVM???????????д??????????????????????????????????????????????????????????????????? ??-??-?? ??????? ??-??-???????????????? ?? ????????? δ?????????????????????? instance ?????? null ????????г???????????????????????? instance????????????????