设计模式——单例模式

单例模式

1. 懒汉式(线程不安全)

懒汉式单例在第一次使用时才创建实例,但这种方式在多线程环境下是不安全的。
public class Singleton {  
    private static Singleton instance;  
  
    private Singleton() {}  
  
    public static Singleton getInstance() {  
        if (instance == null) {  
            instance = new Singleton();  
        }  
        return instance;  
    }  
}

2. 懒汉式(线程安全)

通过在getInstance()方法上添加synchronized关键字,使其在多线程环境下也能正常工作,但这样会降低效率。
public class Singleton {  
    private static Singleton instance;  
  
    private Singleton() {}  
  
    public static synchronized Singleton getInstance() {  
        if (instance == null) {  
            instance = new Singleton();  
        }  
        return instance;  
    }  
}

3. 双重检查锁定

这种方式既保证了线程安全,又避免了同步带来的性能影响。
注意:这里使用了volatile关键字,它禁止了指令重排序,确保了在多线程环境下instance变量的可见性和有序性。
public class Singleton {  
    private static volatile Singleton instance;  
  
    private Singleton() {}  
  
    public static Singleton getInstance() {  
        if (instance == null) {  
            synchronized (Singleton.class) {  
                if (instance == null) {  
                    instance = new Singleton();  
                }  
            }  
        }  
        return instance;  
    }  
}

4. 饿汉式

饿汉式单例在类加载时就完成了初始化,所以类加载较慢,但获取对象的速度快。
public class Singleton {  
    private static final Singleton instance = new Singleton();  
  
    private Singleton() {}  
  
    public static Singleton getInstance() {  
        return instance;  
    }  
}

5. 静态内部类

这种方式利用了类加载机制来保证初始化实例时只有一个线程,并且实现了懒加载。
public class Singleton {  
    private Singleton() {}  
  
    private static class SingletonHolder {  
        private static final Singleton INSTANCE = new Singleton();  
    }  
  
    public static final Singleton getInstance() {  
        return SingletonHolder.INSTANCE;  
    }  
}

6.枚举

通过Java的枚举,可以更简洁地实现单例模式,并且自动支持序列化机制,防止多次实例化。
public enum Singleton {  
    INSTANCE;  
  
    public void whateverMethod() {  
    }  
}

相关推荐

  1. 设计模式

    2024-07-11 22:24:01       59 阅读
  2. 设计模式

    2024-07-11 22:24:01       37 阅读
  3. 设计模式

    2024-07-11 22:24:01       37 阅读
  4. 设计模式

    2024-07-11 22:24:01       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 22:24:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 22:24:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 22:24:01       58 阅读
  4. Python语言-面向对象

    2024-07-11 22:24:01       69 阅读

热门阅读

  1. C# 反射

    2024-07-11 22:24:01       20 阅读
  2. Ubuntu 软件源404not found原因及解决办法

    2024-07-11 22:24:01       17 阅读
  3. 拓扑排序(算法篇)

    2024-07-11 22:24:01       23 阅读
  4. SQL 存储过程

    2024-07-11 22:24:01       24 阅读
  5. 大数据面试题之数据湖

    2024-07-11 22:24:01       21 阅读
  6. MySQL常用命令

    2024-07-11 22:24:01       17 阅读
  7. 多态

    多态

    2024-07-11 22:24:01      22 阅读
  8. 面向本科生的智能品牌传播策略优化

    2024-07-11 22:24:01       27 阅读
  9. 数字化转型

    2024-07-11 22:24:01       15 阅读
  10. MySQL索引之索引类型

    2024-07-11 22:24:01       18 阅读
  11. 在 Linux 上安装 Miniconda

    2024-07-11 22:24:01       23 阅读
  12. 洛谷P7537-字典树+DFS

    2024-07-11 22:24:01       19 阅读