单例设计模式
单例设计模式是一种对象创立模式,用于产生一个对象的具体实例,它可以确保系统中一个类只产生一个实例。
好处:
- 对于频繁使用的对象,可以省略创立对象所花费的时间,这对于那些重量级对象而言,是非常可观的一笔系统开销。
- 因为new操作的次数减少,因此对系统内存的使用频率也会降低,这将减轻GC压力,缩短GC停顿时间。
单例模式的六种写法:
一、饿汉单例设计模式
步骤:
- 私有化构造函数。
- 公告本类的引用类型变量,并且使用该变量指向本类对象。
- 提供一个公共静态的方法获取本类的对象。
//饿汉单例设计模式 ----> 保证Single在在内存中只有一个对象。public class HungrySingleton { //公告本类的引用类型变量,并且使用该变量指向本类对象 private static final HungrySingleton instance = new HungrySingleton(); //私有化构造函数 private HungrySingleton(){ System.out.println("instance is created"); } //提供一个公共静态的方法获取本类的对象 public static HungrySingleton getInstance(){ return instance; }}不足:无法对instance实例做推迟加载
优化:懒汉
二、懒汉单例设计模式
- 私有化构造函数。
- 公告本类的引用类型变量,但是不要创立对象。
- 提供公共静态的方法获取本类的对象,获取之前先判断能否已经创立了本类对象,假如已经创立了,那么直接返回对象就可,假如还没有创立,那么先创立本类的对象,而后再返回。
//懒汉单例设计模式 ----> 保证Single在在内存中只有一个对象。public class LazySingleton { //公告本类的引用类型变量,不创立本类的对象 private static LazySingleton instance; //私有化构造函数 private LazySingleton(){ } public static LazySingleton getInstance(){ //第一次调用的时候会被初始化 if(instance == null){ instance = new LazySingleton(); } return instance; }}不足:在多线程的情况下,无法保证内存中只有一个实例
public class MyThread extends Thread{ @Override public void run() { System.out.println(LazySingleton.getInstance().hashCode()); } public static void main(String[] args) { MyThread[] myThread = new MyThread[10]; for(int i=0;i<myThread.length;i++){ myThread[i] = new MyThread(); } for(int j=0;j<myThread.length;j++){ myThread[j].start(); } }}打印结果:
25768830219834837401983483740198348374019834837401983483740198348374013881389721983483740257688302在多线程并发下这样的实现无法保证明例是唯一的。
优化:懒汉线程安全
三、懒汉线程安全
通过使用同步函数或者者同步代码块保证
public class LazySafetySingleton { private static LazySafetySingleton instance; private LazySafetySingleton(){ } //方法中公告synchronized关键字 public static synchronized LazySafetySingleton getInstance(){ if(instance == null){ instance = new LazySafetySingleton(); } return instance; } //同步代码块实现 public static LazySafetySingleton getInstance1(){ synchronized (LazySafetySingleton.class) { if(instance == null){ instance = new LazySafetySingleton(); } } return instance; }}不足:使用synchronized导致性能缺陷
优化:DCL
四、DCL
DCL:double check lock (双重检查锁机制)
public class DclSingleton { private static DclSingleton instance = null; private DclSingleton(){ } public static DclSingleton getInstance(){ //避免不必要的同步 if(instance == null){ //同步 synchronized (DclSingleton.class) { //在第一次调用时初始化 if(instance == null){ instance = new DclSingleton(); } } } return instance; }}不足:在if判断中执行的instance = new DclSingleton(),该操作不是一个原子操作,JVM首先会按照逻辑,第一步给instance分配内存;第二部,调用DclSingleton()构造方法初始化变量;第三步将instance对象指向JVM分配的内存空间;JVM的缺点:在即时编译器中,存在指令重排序的优化,即以上三步不肯定会按照顺序执行,就会造成线程不安全。
优化:给instance的公告加上volatile关键字,volatile能保证线程在本地不会存有instance的副本,而是每次都到内存中读取。即禁止JVM的指令重排序优化。即按照本来的步骤。把instance公告为volatile之后,对它的写操作就会有一个内存屏障,这样,在它的赋值完成之前,就不会调用读操作。
注意:volatile阻止的不是instance = new DclSingleton();这句话内部的指令排序,而是保证了在一个写操作完成之前,不会调用读操作(if(instance == null))
public class DclSingleton { private static volatile DclSingleton instance = null; private DclSingleton(){ } public static DclSingleton getInstance(){ //避免不必要的同步 if(instance == null){ //同步 synchronized (DclSingleton.class) { //在第一次调用时初始化 if(instance == null){ instance = new DclSingleton(); } } } return instance; }}五、静态内部类
JVM提供了同步控制功能:static final,利用JVM进行类加载的时候保证数据同步。
在内部类中创立对象实例,只需应用中不使用内部类,JVM就不会去加载该类,就不会创立我们要创立的单例对象,
public class StaticInnerSingleton { private StaticInnerSingleton(){ } /** * 在第一次加载StaticInnerSingleton时不会初始化instance, * 只在第一次调用了getInstance方法时,JVM才会加载StaticInnerSingleton并初始化instance * @return */ public static StaticInnerSingleton getInstance(){ return SingletonHolder.instance; } //静态内部类 private static class SingletonHolder{ private static final StaticInnerSingleton instance = new StaticInnerSingleton(); }}优点:JVM本身机制保证了线程安全,没有性能缺陷。
六、枚举
public enum EnumSingleton { //定义一个枚举的元素,它就是Singleton的一个实例 INSTANCE; public void doSomething(){ }}优点:写法简单,线程安全
注意:假如在枚举类中有其余实例方法或者实例变量,必需确保是线程安全的。