Java学习笔记程序员

设计模式-单例模式

2017-06-02  本文已影响28人  KevinLive

原文地址:LoveDev

单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一,该模式确保一个类只有一个实例

单例模式单例模式

懒汉式,线程不安全

class LazySingleton {
    
    private static LazySingleton instance;
    private LazySingleton (){}
    public static LazySingleton getInstance() {
        if (instance == null) {
            instance = new LazySingleton();
        }
        return instance;
    }
}

懒汉式使用懒加载模式,但是在多线程环境中调用 getInstance() 就会创建多个实例

懒汉式,线程安全

class SynchronizedLazySingleton {

    private static SynchronizedLazySingleton instance;
    private SynchronizedLazySingleton(){}
    
    //添加synchronized关键字
    public static synchronized SynchronizedLazySingleton getInstance() {
        if (instance == null) {
            instance = new SynchronizedLazySingleton();
        }
        return instance;
    }
}

getInstance() 方法加上 synchronized 关键字实现线程安全,但是该方法效率上有问题,任何时候只能有一个线程调用 getInstance() 获取实例,而且实例第一次创建之后就不需要同步操作

饿汉式,线程安全

class HungrySingleton {

    private static final HungrySingleton instance = new HungrySingleton();

    private HungrySingleton(){}
    public static HungrySingleton getInstance(){
        return instance;
    }
}

实例被 static final 修饰,类加载时就会初始化,但它不是懒加载模式,如果 HungrySingleton 实例的创建需要某个条件参数,这种写法就不能实现

双重检验锁

class DoubleCheckedSingleton {

    private volatile static DoubleCheckedSingleton instance;

    private DoubleCheckedSingleton() {}

    public static DoubleCheckedSingleton getInstance() {
        if (instance == null) {
            synchronized (DoubleCheckedSingleton.class) {
                if (instance == null) {
                    instance = new DoubleCheckedSingleton();
                }
            }
        }
        return instance;
    }
}

为了避免在多线程环境中执行 instance = new DoubleCheckedSingleton() 造成指令重排序,添加 volatile 关键字修饰,禁止指令重排序优化,抛开代码可读性来说这种写法已经完美了,但是这样你就满足了吗?没有的话就接着看下去吧

静态内部类

class StaticSingleton {

    private static class SingletonHolder {
        private static final StaticSingleton INSTANCE = new StaticSingleton();
    }
    private StaticSingleton (){}
    public static final StaticSingleton getInstance() {
        return SingletonHolder.INSTANCE;
    }
}

该方法为 《Effective Java》上推荐的,它是懒汉式,并且性能没有缺陷

枚举

enum  EnumSingleton {
    INSTANCE
}

最简单的单例类,没有之一,还能防止序列化导致重新创建新的对象

思考

以上介绍的单例模式的写法均在单一进程中有效,如果是多进程环境中,这些写法均不能保证实例的唯一性,如何在多进程环境中保证单例模式的特性呢?

上一篇 下一篇

猜你喜欢

热点阅读