第四章 单例模式

这篇具有很好参考价值的文章主要介绍了第四章 单例模式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


前言

第四章 单例模式
第四章 单例模式
第四章 单例模式


一、单例模式的介绍

第四章 单例模式

二、单例模式的 8 种实现方式(懒汉式要注意线程安全问题)

第四章 单例模式

1、饿汉式(静态常量)

第四章 单例模式

代码示例

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());
    }
}


//饿汉式(静态变量)
class Singleton {
    //1. 构造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本类内部创建对象实例
    private final static Singleton instance = new Singleton();
    //3. 提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance() {
        return instance;
    }
}

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

优缺点:可能会造成内存的浪费,但也只能浪费内存

第四章 单例模式

2、饿汉式(静态代码块)

第四章 单例模式

代码示例


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());
    }
}


//饿汉式(静态变量)
class Singleton {
    //1. 构造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本类内部创建对象实例
//    private final static  Singleton instance = new Singleton();
    private static Singleton instance;

    static {
        //使用静态代码块,不再使用 final 修饰为常量
        instance = new Singleton();
    }

    //3. 提供一个公有的静态方法,返回实例对象
    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());
    }
}


//饿汉式(静态变量)
class Singleton {
    //1. 构造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本类内部创建对象实例
    private static Singleton instance;

    //3. 提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance() {
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

输出

true
instance.hashCode=460141958
instance2.hashCode=460141958

缺点:多线程不安全

第四章 单例模式

4、懒汉式(线程安全,同步方法)添加 synchronized 关键字

第四章 单例模式

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());
    }
}


//饿汉式(静态变量)
class Singleton {
    //1. 构造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本类内部创建对象实例
    private static Singleton instance;

    //3. 提供一个公有的静态方法,返回实例对象
    /** 添加  synchronized 关键字保证线程安全 */
    public static synchronized Singleton getInstance() {
        if(instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

输出

true
instance.hashCode=460141958
instance2.hashCode=460141958

缺点效率太低

第四章 单例模式

5、懒汉式(同步代码块)不能使用,解决不了线程安全问题

第四章 单例模式

第四章 单例模式

6、懒汉式(双重检查加 volatile关键字)推荐使用

第四章 单例模式
第四章 单例模式

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());
    }
}


//饿汉式(静态变量)
class Singleton {
    //1. 构造器私有化, 外部能 new
    private Singleton() {
    }
    //2.本类内部创建对象实例,添加 volatile 保证有序性和可见性
    private static volatile Singleton instance;

    //3. 提供一个公有的静态方法,返回实例对象
    /** 双重检查加 volatile 关键字 */
    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、静态内部类

在类加载时,静态内部类没有调用是不会进行类加载的,当被调用时才会被加载,而且只加载一次,加载时线程安全

第四章 单例模式

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());
    }
}


//饿汉式(静态变量)
class Singleton {
    //1. 构造器私有化, 外部能 new
    private Singleton() {
    }

    //提供一个静态内部类,根据JVM底层机制保证线程安全
    private static class SingletonInstance{
        private static final Singleton INSTANCE = new Singleton();
    }

    //3. 提供一个公有的静态方法,返回实例对象
    public static Singleton getInstance() {
        Singleton instance = SingletonInstance.INSTANCE;
        return instance;
    }
}



输出

true
instance.hashCode=460141958
instance2.hashCode=460141958

优缺点

第四章 单例模式

8、使用枚举实现单例懒加载

第四章 单例模式

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 类使用的是单例模式

第四章 单例模式
第四章 单例模式

四、单例模式注意事项和细节说明

第四章 单例模式文章来源地址https://www.toymoban.com/news/detail-420094.html

到了这里,关于第四章 单例模式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 第四章 介绍Productions - 连接选项 - 使用文件适配器的业务主机类

    针对特定场景 IRIS 提供专门的业务服务类和已经使用特定适配器的业务操作类: File adapters FTP adapters HTTP and SOAP adapters TCP adapters 要使用这些业务主机,通常不需要执行任何编码。 或实际原因,以下部分可能不会涵盖 IRIS 提供的所有专业业务主机类。要查找指定适配器的所有

    2024年02月05日
    浏览(28)
  • 【云原生进阶之PaaS中间件】第四章RabbitMQ-1-简介及工作模式

            RabbitMQ 是一个由 Erlang 语言开发的 AMQP 的开源实现。AMQP(Advanced Message Queue:高级消息队列协议)它是应用层协议的一个开放标准,为面向消息的中间件设计,基于此协议的客户端与消息中间件可传递消息,并不受产品、开发语言等条件的限制。RabbitMQ 最初起源于

    2024年02月21日
    浏览(63)
  • 第四章 Linux网络编程 4.1 网络结构模式 4.2MAC地址、IP地址、端口

    C/S结构 简介 服务器 - 客户机 ,即 Client - Server(C/S)结构。C/S 结构通常采取两层结构。服务器负责数据的管理,客户机负责完成与用户的交互任务。客户机是因特网上访问别人信息的机器,服务器则是提供信息供人访问的计算机。 客户机通过局域网与服务器相连,接受用户

    2024年02月08日
    浏览(32)
  • 【Vue3源码】第四章 实现isReadonly和isReactive

    【Vue3源码】第四章 实现isReadonly和isReactive 上一章节我们实现 readonly API,并且优化之前写的 reactive API。这一章我们实现 isReadonly 和 isReactive 两个API。 官网是这么介绍的:检查一个对象是否是由 reactive() 或 shallowReactive() 创建的代理。 原理很简单我们来实现一下这个函数~ 先看

    2024年01月19日
    浏览(32)
  • 第四章 RPC 调用

    通过以上案例我们发现,Http请求调用服务实例属实过于麻烦。其实对于请求同一个服务,很多步骤都是相同的,例如:服务名,地址,httpClient 创建步骤等。 RPC的出现,就是为了解决这一问题。 RPC: 即我们常说的远程过程调用,就是像调用本地方法一样调用远程方法,通信协

    2024年02月04日
    浏览(36)
  • 第四章网关

    Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等响应式编程和事件流技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。 Gateway网关是我们服务的守门神,所有微服务的统一入口。 网关的核

    2024年02月10日
    浏览(40)
  • linux第四章(网络)

    在配置前首先查看本机的ensXX信息:cat ens160.nmconnection 看本机配置:cd /etc/NetworkManager/                       cd  system-connextions/    ls 一。接口管理命令:ip命令/nmcli命令/nmtui命令 1.对IP地址进行操作: ip的命令: IP link:显示网络设备的运行状态 ip -s show ens160:查看设备(en

    2024年01月19日
    浏览(35)
  • 计网:第四章 网络层

    基于湖科大教书匠b站计算机网络教学视频以及本校课程老师ppt 整合出的计算机网络学习笔记 根据文章目录,具体内容都在附赠的pdf文件中,适合日常学习、考前冲刺 一下是第四章笔记中大概的知识点内容,欢迎查漏补缺^^ 可以在电脑网页端进行下载哦~ 目录 1.网络层概述

    2024年01月24日
    浏览(57)
  • 第四章 数组

    可以多看几遍视频 把上课的代码,自己加加注释,在自己写之前,可以画一个流程图 照着流程图把代码自己实现一遍 不要怀疑自己,不要遇到困难就觉得自己不行,遇到困难就解决困难,编程初学者都是这样一步一步走过来的。 在指针阶段会演示整型、浮点型、字符型传递

    2024年02月12日
    浏览(38)
  • 第四章 Text

    在本章中,您将学习如何在页面上绘制文本。 绘图文本是 PDF 图形中最复杂的部分,但它也是帮助 PDF 击败竞争对手成为当今国际标准的原因。 当其他原始播放器将文本转换为光栅图像或矢量路径(以保持视觉完整性)时,PDF 的发明者知道用户需要可以搜索和复制的文本,而

    2024年02月06日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包