????1??????????????
??????????У?????????????????????????????
????Singleton?????????????????????????????????????????????????UML??????????????“???????????????????????”?????????????????Singleton????????????????????????????????????????漰??DCL??????double checked locking??????????漰??????????????ClassLoader??Э?????漰????JVM??????????EJB???????漰?????????????????????
????????
?????????????????????????????????????????


?6.1 ????????UML?

?????????????????????????????
????1.??δ?getInstance()????????????????????
????2.???????????£?getInstance()????????????????????
????3.???????????
????4.???????Lazy Load????????????????????
???????????1?е??????A??
????4.public class SingletonA {   
????5.    
????6.    /** 
????7.     * ??????????? 
????8.     */   
????9.    private static SingletonA instance = null;   
????10.    
????11.    public static SingletonA getInstance() {   
????12.        if (instance == null) {                              //line 12   
????13.            instance = new SingletonA();          //line 13   
????14.        }   
????15.        return instance;   
????16.    }   
????17.}   
???????д????????????????????????????2??????????????????????????????????????????Singleton.getInstance()?????????????ж???instance????null????????е?line 12????line 13??λ?á?????ж?????JVM??CPU????л???????????????????????line 13??????instance????????????????????new Signleton()??????????????????????????????е??????new Signleton()????????????????????????????????2??????
???????????????
???????2?е?????B??
????4.public class SingletonB {   
????5.    
????6.    /** 
????7.     * ??????????? 
????8.     */   
????9.    private static SingletonB instance = null;   
????10.    
????11.    public synchronized static SingletonB getInstance() {   
????12.        if (instance == null) {   
????13.            instance = new SingletonB();   
????14.        }   
????15.        return instance;   
????16.    }   
????17.}   
??????????A??????????ж??????synchronized???η????????????????????????????????????и???????????????????????????????????ε???????????SingletonKerriganB?????????????????ε????????????instance????????????????????????С??????????????????synchronized???η??????????????????????????????
???????3??????C??
????4.public class SingletonC {   
????5.    
????6.    /** 
????7.     * ??????????? 
????8.     */   
????9.    private static SingletonKerriganD instance = null;   
????10.    
????11.    public static SingletonC getInstance() {   
????12.        if (instance == null) {   
????13.            synchronized (SingletonC.class) {   
????14.                if (instance == null) {   
????15.                    instance = new SingletonC();   
????16.                }   
????17.            }   
????18.        }   
????19.        return instance;   
????20.    }   
????21.}   
????????????????????????????????????δ?????????????????????????if?з?????????????????????С????????????
?????????????????????????????????е?instance = new SingletonKerriganD()???????????????仰?????????????????????????????????????????????????????????????б???й?????????????????????????Σ??????????????????????????к??????????????仰?????????JVM??е??????????????仰???????8????????????????3?????飺
????1.??Kerrigan???????????檔
????2.?????Kerrigan???????
????3.??instance?????????????????????instance??null?????
?????????????Java?????????????????????У?out-of-order???????JDK1.5??JMM??Java Memory Medel????Cache???????????????д????漲?????????????????????????????????????????????????1-2-3???????1-3-2?????????????????3???????2δ??????????л???????????????instance????????????????й????????instance?????????????????????????instance?????????????????μ??????????????????????????????????????????????δ??????ó???????????輸????????
????DCL??д?????????????????顢????饗????????JDK1.4????汾???鼮?????????д???????????????????????????Щ????????C???????DCL????е??????????????2??3?????????JDK1.5????????????????????????????JMM?????廯??volatile????????????JDK??1.5??????汾????????instance???????“private volatile static SingletonKerriganD instance = null;”????????ζ??instance????????????????????DCL??д?????????????????volatile??????????????????????????????????JDK1.42???????汾??????????е?????д???????????????
???????????????????????????????豹??棬????JLS??Java Language Specification???е?漲????????????ClassLoader????????????Σ??????JVM????????????????????????????JVM????.
???????4??????D:
????4.public class SingletonD {   
????5.    
????6.    /** 
????7.     * ??????????? 
????8.     */   
????9.    private static SingletonD instance = new SingletonD();   
????10.    
????11.    public static SingletonD getInstance() {   
????12.        return instance;   
????13.    }   
????14.}   
????????д?????????????????????????????????ClassLoader???????Kerrigan??????????????????????????????????Щ?????н?????????????????????????????????????????????getInstance()???????????????????ò????????????????????д???????????
?????????????????E:
????4.public class SingletonE {   
????5.    
????6.    private static class SingletonHolder {   
????7.        /** 
????8.         * ??????????? 
????9.         */   
????10.        static final SingletonE INSTANCE = new SingletonE();   
????11.    }   
????12.    
????13.    public static SingletonE getInstance() {   
????14.        return SingletonHolder.INSTANCE;   
????15.    }   
????16.}   
????????д????????JVM????????????????????????SingletonHolder????е??????getInstance()?????а?????????????????????????????????????????????????????????????????JDK?汾??
????????????????????????????????????????????????????????????????????????????????????????????????У?
????1.???new????????
????2.??????乹????????
????3.??????л???????????
???????????????????????????????private????protected????????????????????????????public????????????????????????static????????????new????????
?????????????????????????????setAccessible?????????private???????????????????????????????????????? ReflectPermission("suppressAccessChecks") ???????e??????????SecurityManager????checkPermission??????????????????????????????????????Щ???飬?????????÷????????к??????????
??????????????????????????????б?????Serializable????????????????????????readResolve()??????????????л????????????????
?????浥????F:
????4.public class SingletonF implements Serializable {   
????5.    
????6.    private static class SingletonHolder {   
????7.        /** 
????8.         * ??????????? 
????9.         */   
????10.        static final SingletonF INSTANCE = new SingletonF();   
????11.    }   
????12.    
????13.    public static SingletonF getInstance() {   
????14.        return SingletonHolder.INSTANCE;   
????15.    }   
????16.    
????17.    /** 
????18.     * private???????????????????????new??????????? 
????19.     */   
????20.    private SingletonF() {   
????21.    }   
????22.    
????23.    /** 
????24.     * readResolve?????????????????л???? 
????25.     */   
????26.    private Object readResolve() {   
????27.        return getInstance();   
????28.    }   
????29.}