我們一起聊聊Java極簡設(shè)計模式:單例模式(Singleton)
- 本章難度:★★☆☆☆
 - 本章重點(diǎn):介紹創(chuàng)建Java單例對象的七種方式,重點(diǎn)掌握哪些創(chuàng)建方式是線程安全的,哪些方式是線程不安全的,并能夠在實(shí)際項(xiàng)目中靈活運(yùn)用設(shè)計模式,編寫可維護(hù)的代碼。
 
大家好,我是冰河~~
今天給大家介紹《Java極簡設(shè)計模式》的第01章,單例設(shè)計模式(Singleton),多一句沒有,少一句不行,用最簡短的篇幅講述設(shè)計模式最核心的知識,好了,開始今天的內(nèi)容。
單例設(shè)計模式
看幾個單例對象的示例代碼,其中有些代碼是線程安全的,有些則不是線程安全的,需要大家細(xì)細(xì)品味,這些代碼也是冰河本人在高并發(fā)環(huán)境下測試驗(yàn)證過的。
- 代碼一:SingletonExample1
 
這個類是懶漢模式,并且是線程不安全的
package io.binghe.concurrency.example.singleton;
/**
 * @author binghe
 * @version 1.0.0
 * @description 懶漢模式,單例實(shí)例在第一次使用的時候進(jìn)行創(chuàng)建,這個類是線程不安全的
 */
public class SingletonExample1 {
    private SingletonExample1(){}
    private static SingletonExample1 instance = null;
    public static SingletonExample1 getInstance(){
        //多個線程同時調(diào)用,可能會創(chuàng)建多個對象
        if (instance == null){
            instance = new SingletonExample1();
        }
        return instance;
    }
}- 代碼二:SingletonExample2
 
餓漢模式,單例實(shí)例在類裝載的時候進(jìn)行創(chuàng)建,是線程安全的
package io.binghe.concurrency.example.singleton;
/**
 * @author binghe
 * @version 1.0.0
 * @description 餓漢模式,單例實(shí)例在類裝載的時候進(jìn)行創(chuàng)建,是線程安全的
 */
public class SingletonExample2 {
    private SingletonExample2(){}
    private static SingletonExample2 instance = new SingletonExample2();
    public static SingletonExample2 getInstance(){
        return instance;
    }
}- 代碼三:SingletonExample3
 
懶漢模式,單例實(shí)例在第一次使用的時候進(jìn)行創(chuàng)建,這個類是線程安全的,但是這個寫法不推薦
package io.binghe.concurrency.example.singleton;
/**
 * @author binghe
 * @version 1.0.0
 * @description 懶漢模式,單例實(shí)例在第一次使用的時候進(jìn)行創(chuàng)建,這個類是線程安全的,但是這個寫法不推薦
 */
public class SingletonExample3 {
    private SingletonExample3(){}
    private static SingletonExample3 instance = null;
    public static synchronized SingletonExample3 getInstance(){
        if (instance == null){
            instance = new SingletonExample3();
        }
        return instance;
    }
}- 代碼四:SingletonExample4
 
懶漢模式(雙重鎖同步鎖單例模式),單例實(shí)例在第一次使用的時候進(jìn)行創(chuàng)建,但是,這個類不是線程安全的?。。。?!
package io.binghe.concurrency.example.singleton;
/**
 * @author binghe
 * @version 1.0.0
 * @description 懶漢模式(雙重鎖同步鎖單例模式)
 *              單例實(shí)例在第一次使用的時候進(jìn)行創(chuàng)建,這個類不是線程安全的
 */
public class SingletonExample4 {
    private SingletonExample4(){}
    private static SingletonExample4 instance = null;
    //線程不安全
    //當(dāng)執(zhí)行instance = new SingletonExample4();這行代碼時,CPU會執(zhí)行如下指令:
    //1.memory = allocate() 分配對象的內(nèi)存空間
    //2.ctorInstance() 初始化對象
    //3.instance = memory 設(shè)置instance指向剛分配的內(nèi)存
    //單純執(zhí)行以上三步?jīng)]啥問題,但是在多線程情況下,可能會發(fā)生指令重排序。
    // 指令重排序?qū)尉€程沒有影響,單線程下CPU可以按照順序執(zhí)行以上三個步驟,但是在多線程下,如果發(fā)生了指令重排序,則會打亂上面的三個步驟。
    //如果發(fā)生了JVM和CPU優(yōu)化,發(fā)生重排序時,可能會按照下面的順序執(zhí)行:
    //1.memory = allocate() 分配對象的內(nèi)存空間
    //3.instance = memory 設(shè)置instance指向剛分配的內(nèi)存
    //2.ctorInstance() 初始化對象
    //假設(shè)目前有兩個線程A和B同時執(zhí)行g(shù)etInstance()方法,A線程執(zhí)行到instance = new SingletonExample4(); B線程剛執(zhí)行到第一個 if (instance == null){處,
    //如果按照1.3.2的順序,假設(shè)線程A執(zhí)行到3.instance = memory 設(shè)置instance指向剛分配的內(nèi)存,此時,線程B判斷instance已經(jīng)有值,就會直接return instance;
    //而實(shí)際上,線程A還未執(zhí)行2.ctorInstance() 初始化對象,也就是說線程B拿到的instance對象還未進(jìn)行初始化,這個未初始化的instance對象一旦被線程B使用,就會出現(xiàn)問題。
    public static SingletonExample4 getInstance(){
        if (instance == null){
            synchronized (SingletonExample4.class){
                if(instance == null){
                    instance = new SingletonExample4();
                }
            }
        }
        return instance;
    }
}線程不安全分析如下:
當(dāng)執(zhí)行instance = new SingletonExample4();這行代碼時,CPU會執(zhí)行如下指令:
1.memory = allocate() 分配對象的內(nèi)存空間 2.ctorInstance() 初始化對象 3.instance = memory 設(shè)置instance指向剛分配的內(nèi)存
單純執(zhí)行以上三步?jīng)]啥問題,但是在多線程情況下,可能會發(fā)生指令重排序。
指令重排序?qū)尉€程沒有影響,單線程下CPU可以按照順序執(zhí)行以上三個步驟,但是在多線程下,如果發(fā)生了指令重排序,則會打亂上面的三個步驟。
如果發(fā)生了JVM和CPU優(yōu)化,發(fā)生重排序時,可能會按照下面的順序執(zhí)行:
1.memory = allocate() 分配對象的內(nèi)存空間 3.instance = memory 設(shè)置instance指向剛分配的內(nèi)存 2.ctorInstance() 初始化對象
假設(shè)目前有兩個線程A和B同時執(zhí)行g(shù)etInstance()方法,A線程執(zhí)行到instance = new SingletonExample4(); B線程剛執(zhí)行到第一個 if (instance == null){處,如果按照1.3.2的順序,假設(shè)線程A執(zhí)行到3.instance = memory 設(shè)置instance指向剛分配的內(nèi)存,此時,線程B判斷instance已經(jīng)有值,就會直接return instance;而實(shí)際上,線程A還未執(zhí)行2.ctorInstance() 初始化對象,也就是說線程B拿到的instance對象還未進(jìn)行初始化,這個未初始化的instance對象一旦被線程B使用,就會出現(xiàn)問題。
- 代碼五:SingletonExample5
 
懶漢模式(雙重鎖同步鎖單例模式)單例實(shí)例在第一次使用的時候進(jìn)行創(chuàng)建,這個類是線程安全的,使用的是 volatile + 雙重檢測機(jī)制來禁止指令重排達(dá)到線程安全
package io.binghe.concurrency.example.singleton;
/**
 * @author binghe
 * @version 1.0.0
 * @description 懶漢模式(雙重鎖同步鎖單例模式)
 *              單例實(shí)例在第一次使用的時候進(jìn)行創(chuàng)建,這個類是線程安全的
 */
public class SingletonExample5 {
    private SingletonExample5(){}
    //單例對象  volatile + 雙重檢測機(jī)制來禁止指令重排
    private volatile static SingletonExample5 instance = null;
    public static SingletonExample5 getInstance(){
        if (instance == null){
            synchronized (SingletonExample5.class){
                if(instance == null){
                    instance = new SingletonExample5();
                }
            }
        }
        return instance;
    }
}- 代碼六:SingletonExample6
 
餓漢模式,單例實(shí)例在類裝載的時候(使用靜態(tài)代碼塊)進(jìn)行創(chuàng)建,是線程安全的
package io.binghe.concurrency.example.singleton;
/**
 * @author binghe
 * @version 1.0.0
 * @description 餓漢模式,單例實(shí)例在類裝載的時候進(jìn)行創(chuàng)建,是線程安全的
 */
public class SingletonExample6 {
    private SingletonExample6(){}
    private static SingletonExample6 instance = null;
    static {
        instance = new SingletonExample6();
    }
    public static SingletonExample6 getInstance(){
        return instance;
    }
}- 代碼七:SingletonExample7
 
枚舉方式進(jìn)行實(shí)例化,是線程安全的,此種方式也是線程最安全的
package io.binghe.concurrency.example.singleton;
/**
 * @author binghe
 * @version 1.0.0
 * @description 枚舉方式進(jìn)行實(shí)例化,是線程安全的,此種方式也是線程最安全的
 */
public class SingletonExample7 {
    private SingletonExample7(){}
    public static SingletonExample7 getInstance(){
        return Singleton.INSTANCE.getInstance();
    }
    private enum Singleton{
        INSTANCE;
        private SingletonExample7 singleton;
        //JVM保證這個方法絕對只調(diào)用一次
        Singleton(){
            singleton = new SingletonExample7();
        }
        public SingletonExample7 getInstance(){
            return singleton;
        }
    }
}














 
 
 















 
 
 
 