第四章 单例模式

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


前言

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


一、单例模式的介绍

第四章 单例模式

二、单例模式的 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日
    浏览(8)
  • 【云原生进阶之PaaS中间件】第四章RabbitMQ-1-简介及工作模式

    【云原生进阶之PaaS中间件】第四章RabbitMQ-1-简介及工作模式

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

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

    第四章 Linux网络编程 4.1 网络结构模式 4.2MAC地址、IP地址、端口

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

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

    【Vue3源码】第四章 实现isReadonly和isReactive

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

    2024年01月19日
    浏览(11)
  • Linux第四章

    Linux第四章

    ctrl+c :强制停止(Linux某些程序的运行;命令输入错误) ctrl+d :退出或登出(退出账户的登录;或者退出某些特定程序的专属页面) history :查看历史输入过的命令(!命令前缀,自动执行上一次匹配前缀的命令,不能搜索太久的) ctrl+r :输入内容去匹配历史命令(如果搜索到的内

    2024年02月01日
    浏览(9)
  • 第四章——数学知识1

    第四章——数学知识1

    质数:在大于1的整数中,如果只包含1和本身这俩个约束,就被叫质数或素数。 质数的判定——试除法:如果d能整除n,则n/d再除n,结果是一个整数。 d≤n/d。 质因数:一个正整数的俩个因数都是质数 分解质因数——试除法: 从小到大枚举所有的质因数,这里我们要的是质

    2023年04月26日
    浏览(7)
  • 第四章网关

    第四章网关

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

    2024年02月10日
    浏览(9)
  • 第四章,登录注册

    目录 4.1 添加注册页面 4.2 注册表单验证 4.3提交注册信息 4.4 完善注册功能

    2024年02月12日
    浏览(6)
  • 第四章 数组

    第四章 数组

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

    2024年02月12日
    浏览(10)
  • 第四章 路由基础

    第四章 路由基础

    目录 4.1 路由器概述 4.1.1 路由器定义 4.1.2 路由器工作原理 4.1.3 路由表的生成方式 (1)直连路由 (2)静态路由 (3)动态路由 4.1.4 路由器的接口 (1)配置接口 (2)局域网接口 (3)广域网接口 4.1.5 路由器的硬件连接 (1)局域网线缆:双绞线 (2)广域网接口 (3)配置专

    2024年02月08日
    浏览(13)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包