溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java單例如何實現(xiàn)

發(fā)布時間:2021-09-10 13:22:08 來源:億速云 閱讀:157 作者:小新 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)Java單例如何實現(xiàn),小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

單例模式,顧名思義,就是全局只保存有一個實例并且能夠避免用戶去手動實例化,所以單例模式的各種寫法都有一個共同點,不能通過new關(guān)鍵字去創(chuàng)建對象,因此,如果能夠通過構(gòu)造方法實例化,那么就一定要將其聲明為私有。

餓漢式

public class PersonResource {
    public static final PersonResource PERSON_RESOURCE_SINGLETON = new PersonResource();
    private PersonResource(){}
    public  static PersonResource getInstance() {
        return PERSON_RESOURCE_SINGLETON;
    }
}

這種方式可以說是最安全,也最簡單的了,但卻有一個缺點,那就是無論這個實例有沒有被使用到,都會被實例化,頗有些浪費資源

懶漢式一

既然前一種方法有些浪費資源,那就換一種寫法,讓類在被調(diào)用的時候?qū)嵗?/p>

public class PersonResource {
    private static PersonResource personResourceSingleton;
    private PersonResource() {
    }
    public static PersonResource getPersonResourceSingleton(){
        if(null==personResourceSingleton){
            personResourceSingleton = new PersonResource();
        }
        return personResourceSingleton;
    }
}

這種方式能夠在需要用到該實例的時候再初始化,也能夠在單線程下很好的運行,但如果是多線程就容易出現(xiàn)問題了。

懶漢式二

public class PersonResource {
    private static PersonResource personResourceSingleton;
    private PersonResource() {
    }
    public static PersonResource getPersonResourceSingleton(){
        if(null==personResourceSingleton){
            personResourceSingleton = new PersonResource();
        }
        return personResourceSingleton;
    }
}

多線程之所以會出現(xiàn)問題,是因為多個線程能夠并發(fā)執(zhí)行g(shù)etPersonResourceSingleton方法,從而導致在判斷是否為空時出現(xiàn)問題。

既然如此,加上鎖 ,使其互斥即可。這里又出現(xiàn)了一個問題,每次獲取實例的時候都需要加鎖解鎖,而當一個實例已經(jīng)被產(chǎn)生后,再加鎖就有些多余了;

懶漢式三(雙重檢查)

public class PersonResource {
    private PersonResource(){    }
    private volatile static PersonResource personResource;
    public  static PersonResource getInstance(){
        if(personResource==null){
            synchronized (PersonResource.class){
                if(personResource==null){
                    personResource = new PersonResource();
                }
            }
        }
        return personResource;
    }
}

既然實例確定產(chǎn)生后不再需要加鎖,那我們在獲取鎖前先判斷一次是否已經(jīng)有實例存在就可以解決問題了

靜態(tài)內(nèi)部類

public class PersonResource {
    private PersonResource(){}
    private static class PersonResourceHolder{
        public static PersonResource personResourceSingleton = new PersonResource();
    }
    public static PersonResource getInstance(){
        return PersonResourceHolder.personResourceSingleton;
    }
}

除了雙重檢查能夠保證安全的單例外,用一個靜態(tài)內(nèi)部類去持有單例也是可以的,靜態(tài)內(nèi)部類保證了不會隨外部類的加載而加載,這保證了延遲加載,同時在加載該類的時候就實例化單例,保證了線程安全;

枚舉

public enum PersonResource {
    /**
     * PersonResource單例
     */
    personResource;
    public void setPersonResource(){
    }
}

關(guān)于“Java單例如何實現(xiàn)”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI