23种设计模式之一(单例模式)
23种设计模式之一(单例模式)
概念
一个类永远只有一个对象存在。
实现方式
方式1(饿汉式 - 线程安全)
package mirale.luna.design.patterns.singleton; /** * Created by Miracle Luna on 2020/12/15 * * 饿汉式:线程安全 * 类加载到内存后,就实例化一个单例,JVM保证线程安全 * 简单易用,推荐使用! * 唯一缺点:不管用到与否,类装载时就完成实例化 */ public class Singleton01 { private static Singleton01 instance = new Singleton01(); private Singleton01(){} public static Singleton01 getInstance(){ return instance; } /** * 通过查看 hashCode值 是否一致,判断是否线程安全 * @param args */ public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> System.out.println(Singleton01.getInstance().hashCode())).start(); } } }
方式2(懒汉式 - 线程不安全)
package mirale.luna.design.patterns.singleton; /** * Created by Miracle Luna on 2020/12/15 * * 懒汉式:线程不安全 */ public class Singleton02 { private static Singleton02 instance; private Singleton02() {} public static Singleton02 getInstance() { instance = new Singleton02(); return instance; } /** * 通过查看 hashCode值 是否一致,判断是否线程安全 * @param args */ public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> System.out.println(Singleton02.getInstance().hashCode())).start(); } } }
方式3(懒汉式 - 线程安全 - 加锁 - 效率下降)
package mirale.luna.design.patterns.singleton; /** * Created by Miracle Luna on 2020/12/15 * * 懒汉式:线程安全(加锁实现,但是效率下降) */ public class Singleton03 { private static Singleton03 instance; private Singleton03() {} public static synchronized Singleton03 getInstance() { if (instance == null) { instance = new Singleton03(); } return instance; } /** * 通过查看 hashCode值 是否一致,判断是否线程安全 * @param args */ public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> System.out.println(Singleton03.getInstance().hashCode())).start(); } } }
方式4(懒汉式 - 线程安全 - DCL - 效率提升 - 较完美的实现方式)
package mirale.luna.design.patterns.singleton; /** * Created by Miracle Luna on 2020/12/15 * * 懒汉式:线程安全 * DCL: Double Check Loading * 较为完美的单例写法 */ public class Singleton04 { private static volatile Singleton04 instance; // volatile:防止指令重排 private Singleton04() {} public static Singleton04 getInstance() { if (instance == null) { // 双重检查 synchronized (Singleton04.class) { if (instance == null) { instance = new Singleton04(); } } } return instance; } /** * 通过查看 hashCode值 是否一致,判断是否线程安全 * @param args */ public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> System.out.println(Singleton04.getInstance().hashCode())).start(); } } }
方式5(内部类实现 - 线程安全 - 懒加载 - 较完美的实现方式)
package mirale.luna.design.patterns.singleton; /** * Created by Miracle Luna on 2020/12/15 * * 静态内部类 * JVM 保证单例,且线程安全,不用加锁 * 加载外部类时不会加载内部类,变相实现了懒加载(Lazy Loading) * 较为完美的单例写法 */ public class Singleton05 { private Singleton05() {} private static class Singleton05Holder { private static final Singleton05 instance = new Singleton05(); } public static Singleton05 getInstance() { return Singleton05Holder.instance; } /** * 通过查看 hashCode值 是否一致,判断是否线程安全 * @param args */ public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> System.out.println(Singleton05.getInstance().hashCode())).start(); } } }
方式6(枚举类实现 - 线程安全 - 防止反序列化 - 最完美的实现方式)
package mirale.luna.design.patterns.singleton; /** * Created by Miracle Luna on 2020/12/15 * * 枚举单例 * 不仅实现了线程同步,而且防止了反序列化 * 最完美的单例写法 */ public enum Singleton06 { instance; /** * 通过查看 hashCode值 是否一致,判断是否线程安全 * @param args */ public static void main(String[] args) { for (int i = 0; i < 100; i++) { new Thread(() -> System.out.println(Singleton06.instance.hashCode())).start(); } } }
分类:
Java
, Design Patterns
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2019-12-15 IPv6地址表示方式
2019-12-15 MySQL truncate()函数的使用说明