Java中单例模式的几种实现

时间:2024-07-25 14:04:26

懒汉式单例

简单版本

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

缺点:多线程下能够创建多个实例

synchronized版本

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

缺点:给方法加锁,对程序执行效率造成负面影响

双重检查(Double-Check)版本

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

缺点:new Single3()由于编译器的优化,指令重排,还是可能出问题

volatile

public class Single4 {

    private static volatile Single4 instance;

    private Single4() {}

    public static Single4 getInstance() {

        if (instance == null) {

            synchronized (Single4.class) {

                if (instance == null) {

                    instance = new Single4();

                }

            }

        }

        return instance;

    }

}

volatile禁止指令重排

饿汉式单例

实现1

public class SingleB {

    private static final SingleB INSTANCE = new SingleB();

    private SingleB() {}

    public static SingleB getInstance() {

        return INSTANCE;

    }

}

缺点:无法掌握INSTANCE初始化时机,如果依赖其他数据,很难保证其他数据在INSTANCE初始化前准备好。

其他实现方式

静态内部类-Effective Java

public class Singleton {

    private static class SingletonHolder {

        private static final Singleton INSTANCE = new Singleton();

    }

    private Singleton (){}

    public static final Singleton getInstance() {

        return SingletonHolder.INSTANCE;

    }

}
  • 对于内部类SingletonHolder,它是一个饿汉式的单例实现,在SingletonHolder初始化的时候会由ClassLoader来保证同步,使INSTANCE是一个真·单例。

  • 同时,由于SingletonHolder是一个内部类,只在外部类的Singleton的getInstance()中被使用,所以它被加载的时机也就是在getInstance()方法第一次被调用的时候。

枚举-Effective Java2

public enum SingleInstance {

    INSTANCE;

    public void fun1() { 

        // do something

    }

}

// 使用

SingleInstance.INSTANCE.fun1();

缺点:在需要继承的场景不适用。

来源: 在java中写出完美的单例模式-風の嵂菄