為什么使用單例模式划提?
確保某一個(gè)類只有一個(gè)實(shí)例,避免產(chǎn)生多個(gè)對(duì)象消耗過多的資源彩匕, 如要訪問IO和 數(shù)據(jù)庫等資源
實(shí)現(xiàn)單例模式的幾個(gè)關(guān)鍵點(diǎn):
構(gòu)造函數(shù)不對(duì)外開放腔剂,一般為private
通過一個(gè)靜態(tài)方法或枚舉返回單例類對(duì)象
確保單例類的對(duì)象只有一個(gè),尤其是在多線程環(huán)境下
確保單例類對(duì)象反序列化時(shí)不會(huì)重新構(gòu)建對(duì)象
餓漢模式—空間換取時(shí)間驼仪,線程安全
public class Singleton {
private Singleton() { }
private static Singleton instance = new Singleton();
public static Singleton getInstance() {
return instance;
}
}
懶漢模式
public class Singleton {
private static Singleton instance = null;
private Singleton() {}
public synchronized static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
在getinstance方法中加入了synchronized關(guān)鍵字掸犬,也就是getInstance是一個(gè)同步方法袜漩,是線程安全的,但是這種方式存在性能上的缺陷湾碎,每次調(diào)用getInstance都會(huì)進(jìn)行同步宙攻,消耗不必要的資源
優(yōu)點(diǎn):只有在使用時(shí)才會(huì)被實(shí)例化,在一定程度上節(jié)約了資源
缺點(diǎn):第一次加載時(shí)需要及時(shí)實(shí)例化介褥,反應(yīng)稍慢座掘,最大問題是每次調(diào)用getInstance都進(jìn)行同步,造成不必要的同步開銷
Double check lock 模式
能夠在需要時(shí)才實(shí)例化柔滔,保證線程安全溢陪,只有在第一次調(diào)用時(shí)才進(jìn)行同步,以后調(diào)用getInstance都不會(huì)進(jìn)行同步鎖
public class Singleton {
private static Singleton instance = null;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
getInstance方法中對(duì)instance進(jìn)行了兩次判空睛廊,第一次主要是為了避免不必要的同步形真,第二層則是為了在null的情況下創(chuàng)建實(shí)例
instance = new Singleton();此操作并不是一個(gè)原子操作,這句代碼最終會(huì)被編譯成多條匯編指令
- 給Singleton的實(shí)例分配內(nèi)存
- 調(diào)用Singleton的構(gòu)造函數(shù)超全,初始化成員字段
- 將instance對(duì)象指向分配的內(nèi)存空間(此時(shí)instance就不是null了)
由于Java編譯器允許處理器亂序執(zhí)行咆霜,第2,3條的指令執(zhí)行順序是無法保證的嘶朱,執(zhí)行順序可能為1-2-3或1-3-2蛾坯,假定A線程執(zhí)行的順序?yàn)?-3-2,當(dāng)執(zhí)行到第三條指令時(shí)疏遏,此時(shí)instance已經(jīng)不為null脉课,切換到B線程,判斷instance不為空财异,直接取走了instance下翎,使用時(shí)就會(huì)報(bào)錯(cuò),double check lock模式此時(shí)就失效了宝当。
在Java1.5以后视事,具體化了volatile關(guān)鍵字,volatile會(huì)將修改的變量值立即更新到主存中庆揩,保證變量的可見性俐东,只需將instance定義改為private volatile static Singleton instance = null,就可以保證instance對(duì)象每次都是從主內(nèi)存中讀取订晌。
優(yōu)點(diǎn):資源利用率高虏辫,第一次執(zhí)行g(shù)etInstance時(shí)單例對(duì)象才會(huì)被實(shí)例化
缺點(diǎn):第一次加載時(shí)反應(yīng)稍慢
靜態(tài)內(nèi)部類模式
public class Singleton {
private Singleton() {}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
private static class SingletonHolder {
private static Singleton instance = new Singleton();
}
}
第一次加載Singleton類時(shí)并不會(huì)初始化instance,只有在第一次調(diào)用getInstance方法時(shí)才會(huì)初始化instance實(shí)例锈拨,因此砌庄,第一次調(diào)用getInstance方法會(huì)觸發(fā)虛擬機(jī)加載SingletonHolder類,這種方式不僅能保證線程安全,也能保證實(shí)例對(duì)象的唯一性娄昆,同時(shí)也延遲了單例的實(shí)例化佩微,推薦用此模式
枚舉單例
public enum Singleton {
INSTANCE;
public void doSomething() {
}
}
枚舉在Java中和普通類是一樣的,不僅能夠擁有字段萌焰,還能有自己的方法哺眯,最重要的是默認(rèn)枚舉實(shí)例的創(chuàng)建是線程安全的,并且在任何情況下它都是一個(gè)單例扒俯。
在上述幾種單例模式的實(shí)現(xiàn)中奶卓,在反序列化的情況下他們會(huì)出現(xiàn)重新穿件對(duì)象供鸠,枚舉方式則不會(huì)
通過序列化可以將一個(gè)單例的實(shí)例對(duì)象寫到磁盤玻孟,然后再讀回來,從而有效的獲得一個(gè)實(shí)例穆桂。即使構(gòu)造函數(shù)是私有的掌猛,反序列化時(shí)依然可以通過特殊的途徑去創(chuàng)建類的一個(gè)新實(shí)例瑟幕。反序列化操作提供了一個(gè)很特別的鉤子函數(shù)readResolve,如果要杜絕單例對(duì)象在被反序列化時(shí)重新生成對(duì)象留潦,那么必須加入readResolve函數(shù),也就是在readResolve函數(shù)中將單例對(duì)象返回辣往,而不是重新生成一個(gè)新對(duì)象兔院。
private Object readResolve() {
return SingletonHolder.instance;
}
public class Singleton implements Serializable {
private Singleton() {
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
private static class SingletonHolder {
private static Singleton instance = new Singleton();
}
// private Object readResolve() {
// return SingletonHolder.instance;
// }
public static void main(String[] args) throws IOException, ClassNotFoundException {
Singleton s = Singleton.getInstance();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Singleton.obj"));
oos.writeObject(s);
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream("Singleton.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
Singleton s1 = (Singleton) ois.readObject();
ois.close();
System.out.println(s + "\n" + s1);
}
}
//output
com.miracle.thirdlibsourcecode.Singleton@1d44bcfa
com.miracle.thirdlibsourcecode.Singleton@6acbcfc0
不加鉤子函數(shù)反序列化的時(shí)候則會(huì)創(chuàng)建新的對(duì)象
public class Singleton implements Serializable {
private Singleton() {
}
public static Singleton getInstance() {
return SingletonHolder.instance;
}
private static class SingletonHolder {
private static Singleton instance = new Singleton();
}
private Object readResolve() {
return SingletonHolder.instance;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
Singleton s = Singleton.getInstance();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Singleton.obj"));
oos.writeObject(s);
oos.flush();
oos.close();
FileInputStream fis = new FileInputStream("Singleton.obj");
ObjectInputStream ois = new ObjectInputStream(fis);
Singleton s1 = (Singleton) ois.readObject();
ois.close();
System.out.println(s + "\n" + s1);
}
}
//output
com.miracle.thirdlibsourcecode.Singleton@1d44bcfa
com.miracle.thirdlibsourcecode.Singleton@1d44bcfa
加了鉤子函數(shù)readResolve以后,反序列化就不會(huì)重新創(chuàng)建對(duì)象了
而對(duì)于枚舉站削,并不存在這個(gè)問題坊萝,因?yàn)榧词狗葱蛄谢膊粫?huì)生成新的實(shí)例對(duì)象
通過對(duì)enum反編譯發(fā)現(xiàn)其實(shí)enum是繼承了Enum抽象類,在反序列化時(shí)许起,反射在通過newInstance創(chuàng)建對(duì)象時(shí)十偶,會(huì)檢查該類是否ENUM修飾,如果是則拋出異常园细,反射失敗惦积。