设计模式-单例模式(java实战+性能对比)

文章目录

  • 背景
    • 原理
    • 实战
      • 普通初始化(即普通加载)
      • 后初始化(即懒加载)
        • 方法级同步(不推荐)
        • 双查模式(普通推荐)
        • 类加载的holder模式(强力推荐)
  • 评价

背景

设计模式在java编程中发挥的淋漓尽致,一直被大家推崇,以下博主将开设类似的专栏,对各种设计模式加以阐述,并且通过java来进行实战。

首先,对单例模式进行分析和实现。

原理

单例模式存在的意义

  • 需要使用的资源全局只有一个
  • 频繁创建某个类的对象会导致过多的gc,如果全局只用一个则可以有效避免
  • 等等。。。

在spring里面就用到了众多的单例思想。例如@Component,做一个bean,默认就是单例的。

实战

单例模式的重点是实例初始化的过程,初始化包括直接初始化和后初始化(也就是平常说的懒加载)。

  • 普通初始化,在类加载的时候就进行初始化
  • 后初始化,在使用之前进行初始化(需要考虑到同步,否则可能会在并发的时候实例化多个,导致不必要的bug)

对于懒加载的情况,本博客专门测试对比一下几种方法的性能

普通初始化(即普通加载)

public class SingletonDemo {
	// 类加载的时候直接调用初始化(这样做的唯一缺点只有在启动的时候增加一些时间)
    private static final SingletonDemo instance = new SingletonDemo();

    public static SingletonDemo getInstance() {
        return instance;
    }
}

后初始化(即懒加载)

方法级同步(不推荐)
public class SingletonDemo {
    private static SingletonDemo instance;

    private SingletonDemo() {
        System.out.println("默认构造函数必须要为private,否则别人可以new这个对象");
    }

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

    public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        for (int i = 0; i < 10000000; i++) {
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    getInstance();
                }
            });
        }
        executorService.shutdown();
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }
}

方法级别添加同步,能确保最终只创建一个实例,但有点重了,不但同步了实例的创建过程,而且同步了单例的读取过程和引用的返回过程,明显会降低读取的速度
以下是运行时长统计(1000w次)
设计模式-单例模式(java实战+性能对比)_第1张图片

双查模式(普通推荐)
public class SingletonDemo1 {
    private static volatile SingletonDemo1 instance;

    private SingletonDemo1() {
        System.out.println("默认构造函数必须要为private,否则别人可以new这个对象");
    }

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

    public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        for (int i = 0; i < 10000000; i++) {
            executorService.execute(() -> {
                getInstance();
            });
        }
        executorService.shutdown();
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }
}

设计模式-单例模式(java实战+性能对比)_第2张图片
注意点:

  • 单例成员变量,必须要用volatile修饰,避免java内存模型特点而导致的在线程间不同步现象
  • 第一次null判断,是为了读取(读取操作不必进行sychronized同步)
  • 第二次null判断,是为了创建
类加载的holder模式(强力推荐)

利用内部类的后加载特性。如下代码,Holder类的加载是在getInstance方法调用的时候进行的,类加载的过程天然同步,甚至不用加锁,也不用考虑使用volatile关键字来修饰单例成员变量,同时还完成了懒加载。

  • 代码简单
  • 能实现最高性能
public class SingletonDemo3 {
    private SingletonDemo3() {
        System.out.println("默认构造函数必须要为private,否则别人可以new这个对象");
    }

    private static class Holder {
        private static SingletonDemo3 instance = new SingletonDemo3();
    }

    public static SingletonDemo3 getInstance() {
        return Holder.instance;
    }

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < 10000000; i++) {
            executorService.execute(() -> {
                getInstance();
            });
        }
        executorService.shutdown();
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());
    }
}

设计模式-单例模式(java实战+性能对比)_第3张图片

评价

单例模式不要使用方法锁,最好依靠类加载器的方式实现,双查模式也可以基本满足需求。

你可能感兴趣的:(设计模式,单例模式,懒加载,性能,java,设计模式)