免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
單例模式(3)

簡介:

單例模式是一種簡單的設(shè)計(jì)模式,但是要在程序設(shè)計(jì)中使用好單例模式,卻需要注意幾個(gè)地方。

單例模式意味著在整個(gè)系統(tǒng)中,單例類只能有一個(gè)實(shí)例對象,且需要自行完成實(shí)例化,并始終對外提供同一個(gè)實(shí)例對象。

單例模式實(shí)現(xiàn)方式:

餓漢模式:

public class Singleton {    //餓漢模式是最簡單的實(shí)現(xiàn)方式,在類加載時(shí)就創(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(){        // 在需要的時(shí)候才去創(chuàng)建的單例對象,如采羊例對象已經(jīng)創(chuàng)建,再次調(diào)用 ηewinstance()方法時(shí)        // 將不會重新創(chuàng)建新的單例對象,而是直接返回之前創(chuàng)建的單例對象        if (null == instance){            instance = new Singleton();        }        //返回唯一的實(shí)例對象        return instance;    }}

上述懶漢模式有延遲加載的意思,但是沒有考慮到多線程的情況,在多線程的場景中,出現(xiàn)多個(gè)線程同時(shí)調(diào)用newInstance方法創(chuàng)建單例對象,從而導(dǎo)致系統(tǒng)中出現(xiàn)多個(gè)單例類的實(shí)例,顯然是不符合要求的。

懶漢模式(多線程加鎖版):

public class Singleton {    private static Singleton instance = null;    //私有化構(gòu)造方法,防止外界創(chuàng)建對象    private Singleton(){}    public static synchronized Singleton newInstance(){        // 在需要的時(shí)候才去創(chuàng)建的單例對象,如采羊例對象已經(jīng)創(chuàng)建,再次調(diào)用 ηewinstance()方法時(shí)        // 將不會重新創(chuàng)建新的單例對象,而是直接返回之前創(chuàng)建的單例對象        if (null == instance){            instance = new Singleton();        }        //返回唯一的實(shí)例對象        return instance;    }}

上述加鎖設(shè)計(jì)雖然解決了多線程安全的問題,但是單例在系統(tǒng)中只有一個(gè)實(shí)例,每次取實(shí)例都得進(jìn)行加鎖解鎖操作,這會成為系統(tǒng)的性能瓶頸。

懶漢模式(雙重檢測鎖方式):錯(cuò)誤實(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)建單例對象時(shí),在構(gòu)造方法被調(diào)用 之前,就為該對象分配了內(nèi)存空間并將對象的宇段設(shè)置為默認(rèn)值。此時(shí)線程 A 就可以將分配的內(nèi) 存地址賦值給 instance 宇段了,然而該對象可能還沒有初始化。線程 B 來調(diào)用 newInstance()方法,得 到的就 是未初始 化 完全 的單例對象,這就 會導(dǎo)致系統(tǒng)出 現(xiàn)異常行為 。

為了解決該問題,我們可以使用 volatile關(guān)鍵字修飾 instance字段。 volatile關(guān)鍵字的一個(gè)語義就是禁止指令的重排序優(yōu)化,從而保證 instance 字段被初始化時(shí),單例對象己經(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)字段時(shí),會觸發(fā)類加載,并且同一個(gè)類只加載一次。靜態(tài)內(nèi)部類也是如此,類加載過程由類加載器負(fù)責(zé)加鎖,從而保證線程安全。

 

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java幾種方式實(shí)現(xiàn)單例設(shè)計(jì)模式
Java單例模式深入詳解
【設(shè)計(jì)模式】5. 單例模式(以及多線程、無序?qū)懭搿olatile對單例的影響)
C++實(shí)現(xiàn)單例的5種方法總結(jié)
設(shè)計(jì)模式——單例模式(Singleton)
(轉(zhuǎn))C#單例模式詳解
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服