有很多场景,都需要保证一个类仅有一个实例,为此有一种专门的设计模式:单例模式。
单例模式的几种写法
1. 饿汉式(没有延迟加载,线程安全)
public class Singleton {
private static Singleton instance = new Singleton();
public static Singleton getInstance()
{
return instance;
}
}
2. 静态内部类(延迟加载,线程安全)
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTACE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTACE;
}
}
3. 懒汉式(有延迟加载,线程不安全)
public class Singleton {
private static Singleton instance;;
public static Singleton getInstance()
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
}
懒汉安全式(延迟加载,线程安全,效率低)
public class Singleton {
private static Singleton instance;;
public static synchronized Singleton getInstance()
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
}
加了关键字synchronized之后,能保证多线程安全,但是却做了很多无用功,实际只有第一次初始化的时候需要同步。
加以改进,便产生了双重校验方式。
5.双重校验方式(延迟加载,线程安全,效率OK)
public class Singleton {
private volatile static Singleton instance;;
public static Singleton getInstance()
{
if(instance == null)
{
synchronized (Singleton.class) {
if(instance == null)
{
instance = new Singleton();
}
}
}
return instance;
}
}
为了避免每次都同步的性能损耗,做了两次null检查。确保只有第一次调用单例的时候才会做同步。
但是这在java中可能有问题,因为同步块外面的null检查可能看到已经存在但不完整的实例。
因为java平台内存模型允许无序写入和重排序。在构造函数执行之前,变量instance可能就已经变成非null的了。
Java 5之后可以用volatile关键字解决这一问题。
6. 枚举(目前最好的方式)
public enum Singleton
{
INSTANCE;
//other methods
}
枚举没有延迟加载的问题,也能避免线程同步问题,还能解决单例类序列化问题,目前被认为是最好的方式,但是感觉生疏,不易理解。
参考: