单例模式实现方式

转自https://www.cnblogs.com/rjzheng/p/8946889.html


先看一副漫画:


OK,回顾一下小灰的遭遇,上述漫画中所提出的那些问题主要有以下三点:

    1.为什么静态内部类的单例模式是最推荐的?

    2.如何在反射的情况下保证单例?

    3.如何在反序列化中保证单例?

针对上述三个问题有了这篇文章,以一种循序渐进的方式引出最后一种单例设计模式,希望对大家能够有所帮助。

1.饿汉式

直接上代码:

package singleton;

public class Singleton1 {
    private static Singleton1 instance = new Singleton1();

    private Singleton1 (){}

    public static Singleton1 getInstance() {
        return instance;
    }

}

优点就是线程安全,缺点很明显就是浪费内存,因为它在类加载的时候就做实例化对象的操作了。于是就有了懒汉式的单例模式

2.懒汉式

2.1懒汉式V1

直接上代码:

public class LazySingleton1 {

    private static LazySingleton1 instance;

    private LazySingleton1 (){}

    public static LazySingleton1 getInstance() {
        if (instance == null) {
            instance = new LazySingleton1();
        }
        return instance;
    }
}

然而这一版不是线程安全的,于是乎为了线程安全,就在getInstance()方法上加synchronized修饰符,于是getInstance()方法如下:

public static synchronized LazySingleton1 getInstance() {
    if (instance == null) {
        instance = new LazySingleton1();
    }
    return instance;
}

然而,将synchronized加在方法上对性能是大打折扣(synchronized会造成线程阻塞),于是乎又提出一种双重校验锁的单例设计模式,既保证了线程安全又提高了性能。如下:

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

2.2懒汉式V2

懒汉式V1的最后一个双重校验锁版,不能性能再如何优化,还是使用了synchronized修饰符,既然使用了该修饰符,那么对性能多多少少都会造成一些影响,于是乎懒汉式V2版诞生。不过在讲该版之前,先来复习下内部类的加载机制,代码如下:

public class OuterTest {

    static {
        System.out.println("load outer class...");
    }

    // 静态内部类
    static class StaticInnerTest {
        static {
            System.out.println("load static inner class...");
        }

        static void staticInnerMethod() {
            System.out.println("static inner method...");
        }
    }

    public static void main(String[] args) {
        OuterTest outerTest = new OuterTest(); // 此刻其内部类是否也会被加载?
        System.out.println("===========分割线===========");
        OuterTest.StaticInnerTest.staticInnerMethod(); // 调用内部类的静态方法
    }

}

输出结果:

load outer class...
===========分割线===========
load static inner class...
static inner method

因此有如下结论:

    1.加载一个类时,其内部类不会同时被加载

    2.当且仅当其某个静态成员(静态域、构造器、静态方法等)被调用时,类才被加载

基于上面的结论我们有了懒汉式V2版,代码如下:

public class LazySingleton2 {
    private LazySingleton2() {
    }

    static class SingletonHolder {
        private static final LazySingleton2 instance = new LazySingleton2();
    }

    public static LazySingleton2 getInstance() {
        return SingletonHolder.instance;
    }
}

由于对象实例化是在内部类加载的时候构建的,因此该版是线程安全的(因为在方法中创建对象才存在并发问题,静态内部类随着方法调用而被加载,且只加载一次,不存在并发问题,所以是线程安全的)。另外,在getInstance()方法中没有使用synchronized关键字,因此没有造成多余的性能损耗。当LazySingleton2类加载时,其静态内部类SingletonHolder并没有被加载,因此instance对象并没有初始化。而我们在调用LazySingleton2.getInstance()方法时,内部类SingletonHolder被加载,此时单例对象才被构建。因此,这种写法节约空间,达到懒加载的目的,该版也是众多博客中推荐的版本

ps:其实枚举单例模式也有类似的性能,但是因为可读性的原因,并不是最推荐的版本。

2.3懒汉式V3

然而,懒汉式V2版在反射的作用下,单例结构是会被破坏的,测试代码如下:

public class LazySingleton2Test {
    public static void main(String[] args) {
        //创建第一个实例
        LazySingleton2 instance1 = LazySingleton2.getInstance();
    
        //通过反射创建第二个实例
        LazySingleton2 instance2 = null;
        try {
            Class<LazySingleton2> clazz = LazySingleton2.class;
            Constructor<LazySingleton2> cons = clazz.getDeclaredConstructor();
            cons.setAccessible(true);
            instance2 = cons.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //检查两个实例的hash值
        System.out.println("Instance 1 hash:" + instance1.hashCode());
        System.out.println("Instance 2 hash:" + instance2.hashCode());
    }
}

输出结果:

Instance 1 hash:1694819250
Instance 2 hash:1365202186

根据哈希值可以看出,反射破坏了单例的特性,因此懒汉式V3版诞生了:

public class LazySingleton3 {

    private static boolean initialized = false;

    private LazySingleton3() {
        synchronized (LazySingleton3.class) {
            if (initialized == false) {
                initialized = !initialized;
            } else {
                throw new RuntimeException("单例已被破坏");
            }
        }
    }

    static class SingletonHolder {
        private static final LazySingleton3 instance = new LazySingleton3();
    }

    public static LazySingleton3 getInstance() {
        return SingletonHolder.instance;
    }
}

此时再运行一次测试类时,出现如下提示:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at test.LazySingleton3Test.main(LazySingleton3Test.java:21)
Caused by: java.lang.RuntimeException: 单例已被破坏
    at singleton.LazySingleton3.<init>(LazySingleton3.java:12)
    ... 5 more

这里就保证了反射无法破坏其单例特性。

2.4懒汉式V4

在分布式系统中,有些情况下需要在单例类中实现Serializable接口。这样可以在文件系统中存储它的状态并且在稍后的某一时间点取出。

让我们测试这个懒汉式V3版在序列化和反序列化之后是否仍然保持单例:

先将

public class LazySingleton3

修改为:

public class LazySingleton3 implements Serializable 

测试类如下:

public class LazySingleton3Test {
    public static void main(String[] args) {
        try {
            LazySingleton3 instance1 = LazySingleton3.getInstance();
            ObjectOutput out = null;

            out = new ObjectOutputStream(new FileOutputStream("filename.ser"));
            out.writeObject(instance1);
            out.close();

            //deserialize from file to object
            ObjectInput in = new ObjectInputStream(new FileInputStream("filename.ser"));
            LazySingleton3 instance2 = (LazySingleton3) in.readObject();
            in.close();

            System.out.println("instance1 hashCode=" + instance1.hashCode());
            System.out.println("instance2 hashCode=" + instance2.hashCode());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出结果:

instance1 hashCode=2051450519
instance2 hashCode=1510067370

显然我们又看到了两个实例类。为了避免这个问题我们需要提供readResolve()方法的实现。readResolve()方法代替了从流中读取对象。这就确保了在序列化和反序列化的过程中没人可以创建新的实例。

因此,我们提供懒汉式V4版代码如下:

public class LazySingleton4 implements Serializable {

    private static boolean initialized = false;

    private LazySingleton4() {
        synchronized (LazySingleton4.class) {
            if (initialized == false) {
                initialized = !initialized;
            } else {
                throw new RuntimeException("单例已被破坏");
            }
        }
    }

    static class SingletonHolder {
        private static final LazySingleton4 instance = new LazySingleton4();
    }

    public static LazySingleton4 getInstance() {
        return SingletonHolder.instance;
    }
    
    private Object readResolve() {
        return getInstance();
    }
}

此时,再运行测试类,其输出结果如下:

instance1 hashCode=2051450519
instance2 hashCode=2051450519

总结

本文给出了多个版本的单例模式供我们在项目中使用。实际上在实际项目中一般从懒汉式V2、懒汉式V3、懒汉式V4中,根据实际情况三选一即可,并不是非要选择懒汉式V4作为单例来实现。如果没有特殊需求,懒汉式V2足够用了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值