鍗曚緥妯″紡鐨勫疄鐜?/span>
楗挎眽寮?/span>
-
static 鍙橀噺鍦ㄧ被瑁呰浇鐨勬椂鍊欒繘琛屽垵濮嬪寲
-
澶氫釜瀹炰緥鐨?static 鍙橀噺浼氬叡浜悓涓€鍧楀唴瀛樺尯鍩?
鐢ㄨ繖涓や釜鐭ヨ瘑鐐瑰啓鍑虹殑鍗曚緥绫诲氨鏄タ姹夊紡浜嗭紝鍒濆鍖栫被鐨勬椂鍊欏氨鍒涘缓锛岄ゥ涓嶆嫨椋燂紝楗挎眽
public class Singleton {
//鏋勯€犵鏈夊寲锛岄槻姝㈢洿鎺ew
private Singleton(){}
//闈欐€佸垵濮嬪寲鍣紙static initializer锛変腑鍒涘缓瀹炰緥锛屼繚璇佺嚎绋嬪畨鍏?/span>
private static Singleton instance = new Singleton();
public static Singleton getInstance(){
return instance;
}
}
楗挎眽寮忔槸绾跨▼瀹夊叏鐨勶紝JVM鍦ㄥ姞杞界被鏃堕┈涓婂垱寤哄敮涓€鐨勫疄渚嬪璞★紝涓斿彧浼氳杞戒竴娆°€?/p>
Java 瀹炵幇鐨勫崟渚嬫槸涓€涓櫄鎷熸満鐨勮寖鍥达紝鍥犱负瑁呰浇绫荤殑鍔熻兘鏄櫄鎷熸満鐨勶紝鎵€浠ヤ竴涓櫄鎷熸満閫氳繃鑷繁鐨凜lassLoader 瑁呰浇楗挎眽寮忓疄鐜板崟渚嬬被鐨勬椂鍊欏氨浼氬垱寤轰竴涓被瀹炰緥銆傦紙濡傛灉涓€涓櫄鎷熸満閲屾湁澶氫釜ClassLoader鐨勮瘽锛屽氨浼氭湁澶氫釜瀹炰緥锛?/p>
鎳掓眽寮?/span>
鎳掓眽寮忥紝灏辨槸瀹炰緥鍦ㄧ敤鍒扮殑鏃跺€欐墠鍘诲垱寤猴紝姣旇緝鈥滄噿鈥?/p>
鍗曚緥妯″紡鐨勬噿姹夊紡瀹炵幇鏂瑰紡浣撶幇浜嗗欢杩熷姞杞界殑鎬濇兂锛堝欢杩熷姞杞戒篃绉版噿鍔犺浇Lazy Load锛屽氨鏄竴寮€濮嬩笉瑕佸姞杞借祫婧愭垨鏁版嵁锛岀瓑鍒拌浣跨敤鐨勬椂鍊欐墠鍔犺浇锛?/p>
鍚屾鏂规硶
public class Singleton {
private static Singleton singleton;
private Singleton(){}
//瑙e喅浜嗙嚎绋嬩笉瀹夊叏闂锛屼絾鏄晥鐜囧お浣庝簡锛屾瘡涓嚎绋嬫兂鑾峰緱绫荤殑瀹炰緥鐨勬椂鍊欙紝閮介渶瑕佸悓姝ユ柟娉曪紝涓嶆帹鑽?/span>
public static synchronized Singleton getInstance(){
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
}
鍙岄噸妫€鏌ュ姞閿?/h4>
public class Singleton {
//volatitle鍏抽敭璇嶇‘淇濓紝澶氱嚎绋嬫纭鐞唖ingleton
private static volatile Singleton singleton;
private Singleton(){}
public static Singleton getInstance(){
if(singleton ==null){
synchronized (Singleton.class){
if(singleton == null){
singleton = new Singleton();
}
}
}
return singleton;
}
}
Double-Check 姒傚康锛堣繘琛屼袱娆℃鏌ワ級鏄绾跨▼寮€鍙戜腑缁忓父浣跨敤鐨勶紝涓轰粈涔堥渶瑕佸弻閲嶆鏌ラ攣鍛紵鍥犱负绗竴娆℃鏌ユ槸纭繚涔嬪墠鏄竴涓┖瀵硅薄锛岃€岄潪绌哄璞″氨涓嶉渶瑕佸悓姝ヤ簡锛岀┖瀵硅薄鐨勭嚎绋嬬劧鍚庤繘鍏ュ悓姝ヤ唬鐮佸潡锛屽鏋滀笉鍔犵浜屾绌哄璞℃鏌ワ紝涓や釜绾跨▼鍚屾椂鑾峰彇鍚屾浠g爜鍧楋紝涓€涓嚎绋嬭繘鍏ュ悓姝ヤ唬鐮佸潡锛屽彟涓€涓嚎绋嬪氨浼氱瓑寰咃紝鑰岃繖涓や釜绾跨▼灏变細鍒涘缓涓や釜瀹炰緥鍖栧璞★紝鎵€浠ラ渶瑕佸湪绾跨▼杩涘叆鍚屾浠g爜鍧楀悗鍐嶆杩涜绌哄璞℃鏌ワ紝鎵嶈兘纭繚鍙垱寤轰竴涓疄渚嬪寲瀵硅薄銆?/p>
鍙岄噸妫€鏌ュ姞閿侊紙double checked locking锛夌嚎绋嬪畨鍏ㄣ€佸欢杩熷姞杞姐€佹晥鐜囨瘮杈冮珮
volatile锛歷olatile涓€鑸敤浜庡绾跨▼鐨勫彲瑙佹€э紝杩欓噷鐢ㄦ潵闃叉鎸囦护閲嶆帓锛堥槻姝ew Singleton鏃舵寚浠ら噸鎺掑簭瀵艰嚧鍏朵粬绾跨▼鑾峰彇鍒版湭鍒濆鍖栧畬鐨勫璞★級銆傝volatile 淇グ鐨勫彉閲忕殑鍊硷紝灏嗕笉浼氳鏈湴绾跨▼缂撳瓨锛屾墍鏈夊璇ュ彉閲忕殑璇诲啓閮芥槸鐩存帴鎿嶄綔鍏变韩鍐呭瓨锛屼粠鑰岀‘淇濆涓嚎绋嬭兘姝g‘鐨勫鐞嗚鍙橀噺銆?/p>
鎸囦护閲嶆帓
鎸囦护閲嶆帓鏄寚鍦ㄧ▼搴忔墽琛岃繃绋嬩腑, 涓轰簡鎬ц兘鑰冭檻, 缂栬瘧鍣ㄥ拰CPU鍙兘浼氬鎸囦护閲嶆柊鎺掑簭銆?/p>
Java涓垱寤轰竴涓璞★紝寰€寰€鍖呭惈涓変釜杩囩▼銆傚浜巗ingleton = new Singleton()锛岃繖涓嶆槸涓€涓師瀛愭搷浣滐紝鍦?JVM 涓寘鍚涓嬩笁涓繃绋嬨€?/p>
-
-
璋冪敤 Singleton 鐨勬瀯閫犲嚱鏁版潵鍒濆鍖栨垚鍛樺彉閲忥紝褰㈡垚瀹炰緥
-
灏唖ingleton瀵硅薄鎸囧悜鍒嗛厤鐨勫唴瀛樼┖闂达紙鎵ц瀹岃繖姝?singleton鎵嶆槸闈?null 浜嗭級
浣嗘槸锛岀敱浜嶫VM浼氳繘琛屾寚浠ら噸鎺掑簭锛屾墍浠ヤ笂闈㈢殑绗簩姝ュ拰绗笁姝ョ殑椤哄簭鏄笉鑳戒繚璇佺殑锛屾渶缁堢殑鎵ц椤哄簭鍙兘鏄?1-2-3锛屼篃鍙兘鏄?1-3-2銆傚鏋滄槸 1-3-2锛屽垯鍦?3 鎵ц瀹屾瘯锛? 鏈墽琛屼箣鍓嶏紝琚彟涓€涓嚎绋嬫姠鍗犱簡锛岃繖鏃?instance 宸茬粡鏄潪 null 浜嗭紙浣嗗嵈娌℃湁鍒濆鍖栵級锛屾墍浠ヨ繖涓嚎绋嬩細鐩存帴杩斿洖 instance锛岀劧鍚庝娇鐢紝閭h偗瀹氬氨浼氭姤閿欎簡锛屾墍浠ヨ鍔犲叆 volatile鍏抽敭瀛椼€?/p>
闈欐€佸唴閮ㄧ被
public class Singleton {
private Singleton(){}
private static class SingletonInstance{
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance(){
return SingletonInstance.INSTANCE;
}
}
閲囩敤绫诲姞杞界殑鏈哄埗鏉ヤ繚璇佸垵濮嬪寲瀹炰緥鏃跺彧鏈変竴涓嚎绋嬶紱
闈欐€佸唴閮ㄧ被鏂瑰紡鍦⊿ingleton 绫昏瑁呰浇鐨勬椂鍊欏苟涓嶄細绔嬪嵆瀹炰緥鍖栵紝鑰屾槸鍦ㄨ皟鐢╣etInstance鐨勬椂鍊欙紝鎵嶅幓瑁呰浇鍐呴儴绫籗ingletonInstance ,浠庤€屽畬鎴怱ingleton鐨勫疄渚嬪寲
绫荤殑闈欐€佸睘鎬у彧浼氬湪绗竴娆″姞杞界被鐨勬椂鍊欏垵濮嬪寲锛屾墍浠ワ紝JVM甯垜浠繚璇佷簡绾跨▼鐨勫畨鍏ㄦ€э紝鍦ㄧ被鍒濆鍖栨椂锛屽叾浠栫嚎绋嬫棤娉曡繘鍏?/p>
浼樼偣锛氱嚎绋嬪畨鍏紝鍒╃敤闈欐€佸唴閮ㄧ被瀹炵幇寤惰繜鍔犺浇锛屾晥鐜囪緝楂橈紝鎺ㄨ崘浣跨敤
鏋氫妇
enum Singleton{
INSTANCE;
public void method(){}
}
鍊熷姪JDK5 娣诲姞鐨勬灇涓惧疄鐜板崟渚嬶紝涓嶄粎鍙互閬垮厤澶氱嚎绋嬪悓姝ラ棶棰橈紝杩樿兘闃叉鍙嶅簭鍒楀寲閲嶆柊鍒涘缓鏂扮殑瀵硅薄锛屼絾鏄湪鏋氫妇涓殑鍏朵粬浠讳綍鏂规硶鐨勭嚎绋嬪畨鍏ㄧ敱绋嬪簭鍛樿嚜宸辫礋璐c€傝繕鏈夐槻姝笂闈㈢殑閫氳繃鍙嶅皠鏈哄埗璋冪敤绉佺敤鏋勯€犲櫒銆備笉杩囷紝鐢变簬Java1.5涓墠鍔犲叆enum鐗规€э紝鎵€浠ヤ娇鐢ㄧ殑浜哄苟涓嶅銆?/p>
杩欑鏂瑰紡鏄€奅ffective Java銆?浣滆€匤osh Bloch 鎻愬€$殑鏂瑰紡銆?/p>
鍗曚緥妯″紡鍦↗DK 涓殑婧愮爜鍒嗘瀽
JDK 涓紝java.lang.Runtime
灏辨槸缁忓吀鐨勫崟渚嬫ā寮忥紙楗挎眽寮忥級