單例模式
一個類只能有一個實(shí)例
為什么使用單例?
節(jié)省內(nèi)存襟铭、節(jié)省計算 碌奉、方便管理
Spring 管理的bean默認(rèn)就是單例
常見的單例模式
餓漢模式
飽漢/懶漢模式
雙重檢查,模式
靜態(tài)內(nèi)部類模式
枚舉模式
惡漢模式
缺點(diǎn): 在類裝載時就完成了實(shí)例化寒砖,沒有達(dá)到懶加載的效果赐劣,如果從始至終都沒有使用,那么會存在內(nèi)存浪費(fèi)的問題
優(yōu)點(diǎn): 在類裝載時就完成了實(shí)例化哩都,避免了線程同步問題
/**
* 單例-餓漢模式<br>
*/
public class Hungry {
private static Hungry instance = new Hungry();
private Hungry() {
}
public static Hungry getInstance() {
return instance;
}
}
靜態(tài)代碼塊模式魁兼,優(yōu)缺點(diǎn)與上面是一樣的
/**
* 單例- 惡漢 - 靜態(tài)代碼塊模式<br>
*/
public class Hungry2Static {
private static Hungry2Static instance;
static {
instance = new Hungry2Static();
}
private Hungry2Static() {
}
public static Hungry2Static getInstance() {
return instance;
}
}
飽漢/懶漢模式
在調(diào)用 getInstance 時才初始化,避免了內(nèi)存浪費(fèi)的問題
但這種模式只能在單線程下使用
在多線程模式下漠嵌,如果一個線程進(jìn)入了 if (instance == null) 咐汞,還沒來得及進(jìn)入下一步盖呼,另一個線程也進(jìn)來了,就會存在創(chuàng)建多個實(shí)例的情況
/**
* 單例 - 飽漢/懶漢模式<br>
*/
public class Lazy {
private Lazy() {
}
private static Lazy instance;
public static Lazy getInstance() {
if (instance == null) {
instance = new Lazy();
}
return instance;
}
}
升級
給 getInstance 加上 synchronized 關(guān)鍵字
優(yōu)點(diǎn):保證了多線程下線程安全問題
缺點(diǎn):調(diào)用方法是阻塞性的化撕,性能開銷大几晤,效率太低
/**
* 單例 - 飽漢/懶漢模式<br>
*/
public class Lazy {
private Lazy() {
}
private static Lazy instance;
public synchronized static Lazy getInstance() {
if (instance == null) {
instance = new Lazy();
}
return instance;
}
}
為了調(diào)高效果,所以縮小同步范圍
將 synchronized 關(guān)鍵字從 method 上移到了方法內(nèi)部植阴, 對代碼塊進(jìn)行同步
優(yōu)點(diǎn):縮小了同步范圍蟹瘾,提高了效率
缺點(diǎn):多線程下,還是會存在 并發(fā)問題
當(dāng)線程A進(jìn)入 if (instance == null) 還沒來得及往下執(zhí)行時掠手,線程B也進(jìn)來了憾朴,此時就會產(chǎn)生多個實(shí)例
/**
* 單例 - 飽漢/懶漢模式<br>
*/
public class Lazy {
private Lazy() {
}
private static Lazy instance;
public static Lazy getInstance() {
if (instance == null) {
synchronized (Lazy.class) {
instance = new Lazy();
}
}
return instance;
}
}
再升級
雙重檢查模式
雙重檢查模式
優(yōu)點(diǎn):不僅線程安全,而且延遲加載喷鸽,效果更高
缺點(diǎn):需要使用 volatile 關(guān)鍵字
為什么需要二次檢查众雷?
見前面 懶漢模式的 升級 版的說法
為什么需要 volatile 關(guān)鍵字呢?
避免指令重排做祝,拿到未初始化的對象
instance = new DoubleCheck() 這句并非原子操作
涉及到 三步
1.給 instance 分配內(nèi)存空間
2.調(diào)用 DoubleCheck的構(gòu)造器來初始化 instance
3.將 instance 對象指向分配的內(nèi)存空間(執(zhí)行完這步instance就不為null了)
這里需要注意1-2-3的順序报腔,可能存在 JVM 的優(yōu)化(指令重排)
也就是 2,3的順序不一定能保證
如圖:
[圖片上傳失敗...(image-a153fb-1582641478169)]
/**
* 單例- 雙重檢查模式<br>
*/
public class DoubleCheck {
private DoubleCheck() {
}
private static volatile DoubleCheck instance;
public static DoubleCheck getInstance() {
if (instance == null) {
synchronized (DoubleCheck.class) {
if (instance == null) {
instance = new DoubleCheck();
}
}
}
return instance;
}
}
靜態(tài)內(nèi)部類模式
優(yōu)點(diǎn):
靜態(tài)內(nèi)部類(InnerClass)方式在 InnerStaticClass 被裝載時,并不會被初始化
/**
* 單例-靜態(tài)內(nèi)部類模式<br>
*/
public class InnerStaticClass {
private static InnerStaticClass instance;
private InnerStaticClass() {
}
public static InnerStaticClass getInstance() {
return InnerClass.instance;
}
private static class InnerClass {
private InnerClass() {
}
private static final InnerStaticClass instance = new InnerStaticClass();
}
}
綜上:靜態(tài)內(nèi)部類與雙重檢查模式剖淀,都是一樣的,延遲加載纤房,避免了線程不安全纵隔,并且效率高
但有一個問題就是:無法防止 反序列化以及反射