月薪5万程序员眼中的单例模式

作者:孤独烟,来自:http://rjzheng.cnblogs.com/

引言

其实写这篇文章之前,我犹豫了一下,毕竟单例大家都知道,写这么一篇文章会不会让人觉得老掉牙。后来想想,就当一种记录吧。先来一副漫画吧,如下图所示
640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1
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、懒汉式

(1)懒汉式v1
package singleton;
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加在方法上性能大打折扣(syncrhonized会造成线程阻塞),于是乎又提出一种双重校验锁的单例设计模式,既保证了线程安全,又提高了性能。双重校验锁的getInstance()方法如下所示

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


(2)懒汉式v2

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

package test;
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版,代码如下所示

package singleton;
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:其实枚举单例模式也有类似的性能,但是因为可读性的原因,并不是最推荐的版本。

(3)懒汉式v3

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

package test;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import singleton.LazySingleton2;
/**
* @author zhengrongjun
*/

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版诞生了

package singleton;
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
Instance 1 hash:359023572

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

(3)懒汉式v4

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

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

先将

public class LazySingleton3

修改为

public class LazySingleton3 implements Serializable 

上测试类如下

package test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import singleton.LazySingleton3;
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版代码如下

package singleton;
import java.io.Serializable;
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作为单例来实现。最后,希望大家有所收获。

640?wx_fmt=gif

推荐阅读:

技术:分布式唯一ID极简教程

职场:程序员职业规划

分享:2T架构师学习资料干货分享

觉得有帮助?请转发给更多人!

640?wx_fmt=jpeg

架构师小秘圈,聚集10万架构师的小圈子!不定期分享技术干货,行业秘闻!汇集各类奇妙好玩的话题和流行动向!长按左侧图片,扫码加入架构师微信群!

  • 6
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值