簡介:單例模式是一種簡單的設(shè)計模式,但是要在程序設(shè)計中使用好單例模式,卻需要注意幾個地方。 單例模式意味著在整個系統(tǒng)中,單例類只能有一個實(shí)例對象,且需要自行完成實(shí)例化,并始終對外提供同一個實(shí)例對象。 單例模式實(shí)現(xiàn)方式:餓漢模式:public class Singleton { //餓漢模式是最簡單的實(shí)現(xiàn)方式,在類加載時就創(chuàng)建單例類對象 private static final Singleton instance = new Singleton(); //私有化構(gòu)造方法,防止外界創(chuàng)建對象 private Singleton(){} public static Singleton newInstance(){ //返回唯一的實(shí)例對象 return instance; } } 懶漢模式(單線程版):public class Singleton { private static Singleton instance = null; //私有化構(gòu)造方法,防止外界創(chuàng)建對象 private Singleton(){} public static Singleton newInstance(){ // 在需要的時候才去創(chuàng)建的單例對象,如采羊例對象已經(jīng)創(chuàng)建,再次調(diào)用 ηewinstance()方法時 // 將不會重新創(chuàng)建新的單例對象,而是直接返回之前創(chuàng)建的單例對象 if (null == instance){ instance = new Singleton(); } //返回唯一的實(shí)例對象 return instance; } } 上述懶漢模式有延遲加載的意思,但是沒有考慮到多線程的情況,在多線程的場景中,出現(xiàn)多個線程同時調(diào)用newInstance方法創(chuàng)建單例對象,從而導(dǎo)致系統(tǒng)中出現(xiàn)多個單例類的實(shí)例,顯然是不符合要求的。 懶漢模式(多線程加鎖版):public class Singleton { private static Singleton instance = null; //私有化構(gòu)造方法,防止外界創(chuàng)建對象 private Singleton(){} public static synchronized Singleton newInstance(){ // 在需要的時候才去創(chuàng)建的單例對象,如采羊例對象已經(jīng)創(chuàng)建,再次調(diào)用 ηewinstance()方法時 // 將不會重新創(chuàng)建新的單例對象,而是直接返回之前創(chuàng)建的單例對象 if (null == instance){ instance = new Singleton(); } //返回唯一的實(shí)例對象 return instance; } } 上述加鎖設(shè)計雖然解決了多線程安全的問題,但是單例在系統(tǒng)中只有一個實(shí)例,每次取實(shí)例都得進(jìn)行加鎖解鎖操作,這會成為系統(tǒng)的性能瓶頸。 懶漢模式(雙重檢測鎖方式):錯誤實(shí)現(xiàn)public class Singleton { private static Singleton instance = null; //私有化構(gòu)造方法,防止外界創(chuàng)建對象 private Singleton() { } public static Singleton newInstance() { if (null == instance) {//第一次檢測 synchronized (Singleton.class) {//加鎖 if (null == instance) {//第二次檢測 instance = new Singleton(); } } } return instance; } } 由于指令重排優(yōu)化,可能會導(dǎo)致初始化單例對象和將該對象地址賦值給 instance 字段的順 序與上面 Java 代碼中書 寫 的順序不同 。 例如,線程 A 在創(chuàng)建單例對象時,在構(gòu)造方法被調(diào)用 之前,就為該對象分配了內(nèi)存空間并將對象的宇段設(shè)置為默認(rèn)值。此時線程 A 就可以將分配的內(nèi) 存地址賦值給 instance 宇段了,然而該對象可能還沒有初始化。線程 B 來調(diào)用 newInstance()方法,得 到的就 是未初始 化 完全 的單例對象,這就 會導(dǎo)致系統(tǒng)出 現(xiàn)異常行為 。 為了解決該問題,我們可以使用 volatile關(guān)鍵字修飾 instance字段。 volatile關(guān)鍵字的一個語義就是禁止指令的重排序優(yōu)化,從而保證 instance 字段被初始化時,單例對象己經(jīng)被完全初始化 。 懶漢模式(雙重檢測鎖方式 + volatile):public class Singleton { private static volatile Singleton instance = null; //私有化構(gòu)造方法,防止外界創(chuàng)建對象 private Singleton() { } public static Singleton newInstance() { if (null == instance) {//第一次檢測 synchronized (Singleton.class) {//加鎖 if (null == instance) {//第二次檢測 instance = new Singleton(); } } } return instance; } } 靜態(tài)內(nèi)部類方式(推薦):public class Singleton { private static class SingletonHolder { private static final Singleton instance = new Singleton(); } //私有化構(gòu)造方法,防止外界創(chuàng)建對象 private Singleton() { } public static Singleton newInstance() { return SingletonHolder.instance; } } 熟悉 Java 類加載機(jī)制 的讀者知道,當(dāng)?shù)?nbsp;一次訪 問類中的靜態(tài)字段時,會觸發(fā)類加載,并且同一個類只加載一次。靜態(tài)內(nèi)部類也是如此,類加載過程由類加載器負(fù)責(zé)加鎖,從而保證線程安全。
|
|