| package com.atguigu.singleton.type6; |
| |
| public class SingletonTest06 { |
| |
| public static void main(String[] args) { |
| System.out.println("双重检查"); |
| Singleton instance = Singleton.getInstance(); |
| Singleton instance2 = Singleton.getInstance(); |
| System.out.println(instance == instance2); |
| System.out.println("instance.hashCode=" + instance.hashCode()); |
| System.out.println("instance2.hashCode=" + instance2.hashCode()); |
| |
| } |
| |
| } |
| |
| |
| class Singleton { |
| private static volatile Singleton instance; |
| |
| private Singleton() {} |
| |
| |
| |
| |
| public static synchronized Singleton getInstance() { |
| if(instance == null) { |
| synchronized (Singleton.class) { |
| if(instance == null) { |
| instance = new Singleton(); |
| } |
| } |
| |
| } |
| return instance; |
| } |
| } |
| 1) Double-Check概念是多线程开发中常使用到的,如代码中所示,我们进行了两次if (singleton == null)检查,这样就可以保证线程安全了。 |
| 2) 这样,实例化代码只用执行一次,后面再次访问时,判断if (singleton == null),直接return实例化对象,也避免的反复进行方法同步. |
| 3) 线程安全;延迟加载;效率较高 |
| 4) 结论:在实际开发中,推荐使用这种单例设计模式 |
| package com.atguigu.singleton.type7; |
| |
| public class SingletonTest07 { |
| |
| public static void main(String[] args) { |
| System.out.println("使用静态内部类完成单例模式"); |
| Singleton instance = Singleton.getInstance(); |
| Singleton instance2 = Singleton.getInstance(); |
| System.out.println(instance == instance2); |
| System.out.println("instance.hashCode=" + instance.hashCode()); |
| System.out.println("instance2.hashCode=" + instance2.hashCode()); |
| } |
| |
| } |
| |
| |
| class Singleton { |
| private static volatile Singleton instance; |
| |
| |
| private Singleton() {} |
| |
| |
| private static class SingletonInstance { |
| private static final Singleton INSTANCE = new Singleton(); |
| } |
| |
| |
| |
| public static synchronized Singleton getInstance() { |
| |
| return SingletonInstance.INSTANCE; |
| } |
| } |
| 1) 这种方式采用了类装载的机制来保证初始化实例时只有一个线程。 |
| 2) 静态内部类方式在Singleton类被装载时并不会立即实例化,而是在需要实例化时,调用getInstance方法,才会装载SingletonInstance类,从而完成Singleton的实例化。 |
| 3) 类的静态属性只会在第一次加载类的时候初始化,所以在这里,JVM帮助我们保证了线程的安全性,在类进行初始化时,别的线程是无法进入的。 |
| 4) 优点:避免了线程不安全,利用静态内部类特点实现延迟加载,效率高 |
| 5) 结论:推荐使用 |
| package com.atguigu.singleton.type8; |
| |
| public class SingletonTest08 { |
| public static void main(String[] args) { |
| Singleton instance = Singleton.INSTANCE; |
| Singleton instance2 = Singleton.INSTANCE; |
| System.out.println(instance == instance2); |
| System.out.println(instance.hashCode()); |
| System.out.println(instance2.hashCode()); |
| instance.sayOK(); |
| } |
| } |
| |
| |
| enum Singleton { |
| INSTANCE; |
| public void sayOK() { |
| System.out.println("ok~"); |
| } |
| } |
| 1) 这借助JDK1.5中添加的枚举来实现单例模式。不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。 |
| 2) 这种方式是Effective Java作者Josh Bloch 提倡的方式 |
| 3) 结论:推荐使用 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术