国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

第四章 單例模式

這篇具有很好參考價值的文章主要介紹了第四章 單例模式。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。


前言

第四章 單例模式
第四章 單例模式
第四章 單例模式


一、單例模式的介紹

第四章 單例模式

二、單例模式的 8 種實(shí)現(xiàn)方式(懶漢式要注意線程安全問題)

第四章 單例模式

1、餓漢式(靜態(tài)常量)

第四章 單例模式

代碼示例

package tanchishell.SJMS;

public class SingletonTest01 {

    public static void main(String[] args) {
//測試
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}


//餓漢式(靜態(tài)變量)
class Singleton {
    //1. 構(gòu)造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本類內(nèi)部創(chuàng)建對象實(shí)例
    private final static Singleton instance = new Singleton();
    //3. 提供一個公有的靜態(tài)方法,返回實(shí)例對象
    public static Singleton getInstance() {
        return instance;
    }
}

輸出
true
instance.hashCode=460141958
instance2.hashCode=460141958

優(yōu)缺點(diǎn):可能會造成內(nèi)存的浪費(fèi),但也只能浪費(fèi)內(nèi)存

第四章 單例模式

2、餓漢式(靜態(tài)代碼塊)

第四章 單例模式

代碼示例


package tanchishell.SJMS;

public class SingletonTest01 {

    public static void main(String[] args) {
//測試
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}


//餓漢式(靜態(tài)變量)
class Singleton {
    //1. 構(gòu)造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本類內(nèi)部創(chuàng)建對象實(shí)例
//    private final static  Singleton instance = new Singleton();
    private static Singleton instance;

    static {
        //使用靜態(tài)代碼塊,不再使用 final 修飾為常量
        instance = new Singleton();
    }

    //3. 提供一個公有的靜態(tài)方法,返回實(shí)例對象
    public static Singleton getInstance() {
        return instance;
    }
}

輸出

true
instance.hashCode=460141958
instance2.hashCode=460141958

第四章 單例模式

3、懶漢式(線程不安全)不推薦

第四章 單例模式

代碼示例

package tanchishell.SJMS;

public class SingletonTest01 {

    public static void main(String[] args) {
//測試
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}


//餓漢式(靜態(tài)變量)
class Singleton {
    //1. 構(gòu)造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本類內(nèi)部創(chuàng)建對象實(shí)例
    private static Singleton instance;

    //3. 提供一個公有的靜態(tài)方法,返回實(shí)例對象
    public static Singleton getInstance() {
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

輸出

true
instance.hashCode=460141958
instance2.hashCode=460141958

缺點(diǎn):多線程不安全

第四章 單例模式

4、懶漢式(線程安全,同步方法)添加 synchronized 關(guān)鍵字

第四章 單例模式

package tanchishell.SJMS;

public class SingletonTest01 {

    public static void main(String[] args) {
//測試
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}


//餓漢式(靜態(tài)變量)
class Singleton {
    //1. 構(gòu)造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本類內(nèi)部創(chuàng)建對象實(shí)例
    private static Singleton instance;

    //3. 提供一個公有的靜態(tài)方法,返回實(shí)例對象
    /** 添加  synchronized 關(guān)鍵字保證線程安全 */
    public static synchronized Singleton getInstance() {
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

輸出

true
instance.hashCode=460141958
instance2.hashCode=460141958

缺點(diǎn)效率太低

第四章 單例模式

5、懶漢式(同步代碼塊)不能使用,解決不了線程安全問題

第四章 單例模式

第四章 單例模式

6、懶漢式(雙重檢查加 volatile關(guān)鍵字)推薦使用

第四章 單例模式
第四章 單例模式

package tanchishell.SJMS;

public class SingletonTest01 {

    public static void main(String[] args) {
//測試
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}


//餓漢式(靜態(tài)變量)
class Singleton {
    //1. 構(gòu)造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本類內(nèi)部創(chuàng)建對象實(shí)例,添加 volatile 保證有序性和可見性
    private static volatile Singleton instance;

    //3. 提供一個公有的靜態(tài)方法,返回實(shí)例對象
    /** 雙重檢查加 volatile 關(guān)鍵字 */
    public static Singleton getInstance() {
        if(instance == null){
            synchronized (Singleton.class){
                if(instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}


輸出

true
instance.hashCode=460141958
instance2.hashCode=460141958

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

在類加載時,靜態(tài)內(nèi)部類沒有調(diào)用是不會進(jìn)行類加載的,當(dāng)被調(diào)用時才會被加載,而且只加載一次,加載時線程安全

第四章 單例模式

package tanchishell.SJMS;

public class SingletonTest01 {

    public static void main(String[] args) {
//測試
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
    }
}


//餓漢式(靜態(tài)變量)
class Singleton {
    //1. 構(gòu)造器私有化, 外部能 new
    private Singleton() {
    }

    //提供一個靜態(tài)內(nèi)部類,根據(jù)JVM底層機(jī)制保證線程安全
    private static class SingletonInstance{
        private static final Singleton INSTANCE = new Singleton();
    }

    //3. 提供一個公有的靜態(tài)方法,返回實(shí)例對象
    public static Singleton getInstance() {
        Singleton instance = SingletonInstance.INSTANCE;
        return instance;
    }
}



輸出

true
instance.hashCode=460141958
instance2.hashCode=460141958

優(yōu)缺點(diǎn)

第四章 單例模式

8、使用枚舉實(shí)現(xiàn)單例懶加載

第四章 單例模式

package tanchishell.SJMS;

public class SingletonTest01 {

    public static void main(String[] args) {
//測試
        Singleton instance = Singleton.INSTANCE;
        Singleton instance2 = Singleton.INSTANCE;
        System.out.println(instance == instance2); // true
        System.out.println("instance.hashCode=" + instance.hashCode());
        System.out.println("instance2.hashCode=" + instance2.hashCode());
        instance.method();
    }
}

enum Singleton{
    INSTANCE;
    public void method(){
        System.out.println("hello,world");
    }
}

輸出


true
instance.hashCode=460141958
instance2.hashCode=460141958
hello,world

第四章 單例模式

三、JDKの RunTime 類使用的是單例模式

第四章 單例模式
第四章 單例模式

四、單例模式注意事項(xiàng)和細(xì)節(jié)說明

第四章 單例模式文章來源地址http://www.zghlxwxcb.cn/news/detail-420094.html

到了這里,關(guān)于第四章 單例模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 第四章 介紹Productions - 連接選項(xiàng) - 使用文件適配器的業(yè)務(wù)主機(jī)類

    針對特定場景 IRIS 提供專門的業(yè)務(wù)服務(wù)類和已經(jīng)使用特定適配器的業(yè)務(wù)操作類: File adapters FTP adapters HTTP and SOAP adapters TCP adapters 要使用這些業(yè)務(wù)主機(jī),通常不需要執(zhí)行任何編碼。 或?qū)嶋H原因,以下部分可能不會涵蓋 IRIS 提供的所有專業(yè)業(yè)務(wù)主機(jī)類。要查找指定適配器的所有

    2024年02月05日
    瀏覽(31)
  • 【云原生進(jìn)階之PaaS中間件】第四章RabbitMQ-1-簡介及工作模式

    【云原生進(jìn)階之PaaS中間件】第四章RabbitMQ-1-簡介及工作模式

    ????????RabbitMQ 是一個由 Erlang 語言開發(fā)的 AMQP 的開源實(shí)現(xiàn)。AMQP(Advanced Message Queue:高級消息隊(duì)列協(xié)議)它是應(yīng)用層協(xié)議的一個開放標(biāo)準(zhǔn),為面向消息的中間件設(shè)計(jì),基于此協(xié)議的客戶端與消息中間件可傳遞消息,并不受產(chǎn)品、開發(fā)語言等條件的限制。RabbitMQ 最初起源于

    2024年02月21日
    瀏覽(92)
  • 第四章 Linux網(wǎng)絡(luò)編程 4.1 網(wǎng)絡(luò)結(jié)構(gòu)模式 4.2MAC地址、IP地址、端口

    第四章 Linux網(wǎng)絡(luò)編程 4.1 網(wǎng)絡(luò)結(jié)構(gòu)模式 4.2MAC地址、IP地址、端口

    C/S結(jié)構(gòu) 簡介 服務(wù)器 - 客戶機(jī) ,即 Client - Server(C/S)結(jié)構(gòu)。C/S 結(jié)構(gòu)通常采取兩層結(jié)構(gòu)。服務(wù)器負(fù)責(zé)數(shù)據(jù)的管理,客戶機(jī)負(fù)責(zé)完成與用戶的交互任務(wù)??蛻魴C(jī)是因特網(wǎng)上訪問別人信息的機(jī)器,服務(wù)器則是提供信息供人訪問的計(jì)算機(jī)。 客戶機(jī)通過局域網(wǎng)與服務(wù)器相連,接受用戶

    2024年02月08日
    瀏覽(27)
  • 【Vue3源碼】第四章 實(shí)現(xiàn)isReadonly和isReactive

    【Vue3源碼】第四章 實(shí)現(xiàn)isReadonly和isReactive

    【Vue3源碼】第四章 實(shí)現(xiàn)isReadonly和isReactive 上一章節(jié)我們實(shí)現(xiàn) readonly API,并且優(yōu)化之前寫的 reactive API。這一章我們實(shí)現(xiàn) isReadonly 和 isReactive 兩個API。 官網(wǎng)是這么介紹的:檢查一個對象是否是由 reactive() 或 shallowReactive() 創(chuàng)建的代理。 原理很簡單我們來實(shí)現(xiàn)一下這個函數(shù)~ 先看

    2024年01月19日
    瀏覽(18)
  • linux第四章(網(wǎng)絡(luò))

    linux第四章(網(wǎng)絡(luò))

    在配置前首先查看本機(jī)的ensXX信息:cat ens160.nmconnection 看本機(jī)配置:cd /etc/NetworkManager/ ? ? ? ? ? ? ? ? ? ? ? cd? system-connextions/? ? ls 一。接口管理命令:ip命令/nmcli命令/nmtui命令 1.對IP地址進(jìn)行操作: ip的命令: IP link:顯示網(wǎng)絡(luò)設(shè)備的運(yùn)行狀態(tài) ip -s show ens160:查看設(shè)備(en

    2024年01月19日
    瀏覽(21)
  • 第四章網(wǎng)關(guān)

    第四章網(wǎng)關(guān)

    Spring Cloud Gateway 是 Spring Cloud 的一個全新項(xiàng)目,該項(xiàng)目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等響應(yīng)式編程和事件流技術(shù)開發(fā)的網(wǎng)關(guān),它旨在為微服務(wù)架構(gòu)提供一種簡單有效的統(tǒng)一的 API 路由管理方式。 Gateway網(wǎng)關(guān)是我們服務(wù)的守門神,所有微服務(wù)的統(tǒng)一入口。 網(wǎng)關(guān)的核

    2024年02月10日
    瀏覽(24)
  • 第四章 路由基礎(chǔ)

    第四章 路由基礎(chǔ)

    目錄 4.1 路由器概述 4.1.1 路由器定義 4.1.2 路由器工作原理 4.1.3 路由表的生成方式 (1)直連路由 (2)靜態(tài)路由 (3)動態(tài)路由 4.1.4 路由器的接口 (1)配置接口 (2)局域網(wǎng)接口 (3)廣域網(wǎng)接口 4.1.5 路由器的硬件連接 (1)局域網(wǎng)線纜:雙絞線 (2)廣域網(wǎng)接口 (3)配置專

    2024年02月08日
    瀏覽(29)
  • 第四章 磁盤設(shè)備

    第四章 磁盤設(shè)備 一、 關(guān)于掛裝的基本常識 ?與 DOS/Windows 采用驅(qū)動器標(biāo)識符(A:、B:、C:)使用磁 盤設(shè)備的方法不同。Linux 采用單根目錄樹管理全部文件系 統(tǒng)。磁盤設(shè)備必須掛載到系統(tǒng)目錄樹上才能使用。 (Linux 啟動過程已完成對/、/ boot 和/swap 三個分區(qū)的掛裝) ?所謂掛

    2024年02月03日
    瀏覽(28)
  • 第四章-邊界安全

    第四章-邊界安全

    1)什么是防火墻 墻,始于防,忠于守。從古至今,墻予人以安全之意。 防御外網(wǎng)對內(nèi)網(wǎng)的入侵 防火墻是一種 網(wǎng)絡(luò)安全設(shè)備或系統(tǒng) ,用于監(jiān)控和控制網(wǎng)絡(luò)流量,防止未經(jīng)授權(quán)的訪問和攻擊。防火墻可以根據(jù)預(yù)定的規(guī)則和策略,過濾入站和出站數(shù)據(jù)包,保護(hù)網(wǎng)絡(luò)的安全性和完

    2024年01月19日
    瀏覽(40)
  • 第四章 數(shù)組

    第四章 數(shù)組

    可以多看幾遍視頻 把上課的代碼,自己加加注釋,在自己寫之前,可以畫一個流程圖 照著流程圖把代碼自己實(shí)現(xiàn)一遍 不要懷疑自己,不要遇到困難就覺得自己不行,遇到困難就解決困難,編程初學(xué)者都是這樣一步一步走過來的。 在指針階段會演示整型、浮點(diǎn)型、字符型傳遞

    2024年02月12日
    瀏覽(33)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包