当你看到这个标题的时候,以为我是一个标题党?(其实也是…)
但是你真的不能小瞧这个单例模式,不信的话,你可以继续往下看。
小白些单例模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class Singleton {
private static Singleton instance = null;
private Singleton() { }
public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); System.out.println("我开始new对象了~"); } return instance; } }
|
这种单例估计是我们第一眼就能想到的,咋一眼看没问题,因为我们的大脑一眼反应我们的程序都是单线程的。实际上我们系统在初始化的时候就有可能存在多线程的情况。我们模拟并发写一个小程序来验证下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
public class TestCase {
public static void main(String[] args) { for (int i = 0; i < 10; i++) { Thread thread = new Thread(new Runnable() { @Override public void run() { Singleton instance = Singleton.getInstance(); System.out.println(instance); } }); thread.start(); } } }
|
看一眼运行的结果:
1 2 3 4 5 6 7 8 9 10 11 12
| 我开始new对象了~ com.vernon.test.designpattern.singleton.Singleton@773d663a com.vernon.test.designpattern.singleton.Singleton@a963d08 我开始new对象了~ com.vernon.test.designpattern.singleton.Singleton@773d663a com.vernon.test.designpattern.singleton.Singleton@773d663a com.vernon.test.designpattern.singleton.Singleton@773d663a com.vernon.test.designpattern.singleton.Singleton@773d663a com.vernon.test.designpattern.singleton.Singleton@773d663a com.vernon.test.designpattern.singleton.Singleton@773d663a com.vernon.test.designpattern.singleton.Singleton@773d663a com.vernon.test.designpattern.singleton.Singleton@773d663a
|
很明显,new了2次对象,看打印的对象地址也是不对的。
那我们如何去改进呢?– 加锁
加synchronized锁
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
public class Singleton2 {
private static Singleton2 instance = null;
private Singleton2() { } public static synchronized Singleton2 getInstance() { if (instance == null) { instance = new Singleton2(); System.out.println("我开始new对象了~"); } return instance; } }
|
这个确实能解决掉问题,但是后续每次获取它的时候每次都要加锁排队,性能存在一定的问题。
Double Check
那是不是在方法里面做2次校验就能解决掉问题呢?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
public class Singleton3 {
private static Singleton3 instance = null;
private Singleton3() { } public static Singleton3 getInstance() { if (instance == null) { synchronized (Singleton3.class) { if (instance == null) { instance = new Singleton3(); System.out.println("我开始new对象了~"); } } } return instance; } }
|
从代码看上去,不管是单线程还是多线程,起码在instance==null
的时候都能被hold住。但是表现懵逼了我们的双眼。如果这里发生了重排序,就会存在问题。
创建一个对象可以划分为3步:
- 1、分配内存空间;
- 2、初始化对象;
- 3、将内存空间的地址赋值给对象的引用;
如果发生重排序的话,步骤2~3有可能颠倒过来。
防止重排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
public class Singleton4 { private static volatile Singleton4 instance = null;
private Singleton4() { }
public static Singleton4 getInstance() { if (instance == null) { synchronized (Singleton4.class) { if (instance == null) { instance = new Singleton4(); System.out.println("我开始new对象了~"); } } } return instance; } }
|
在这里加上volatile关键字就可以很好的解决了。传送门:https://mp.weixin.qq.com/s/HtMphMK8lyrieg-663q9og
静态内部类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public class Singleton5 {
private static class SingletonHolder { public static Singleton5 instance = new Singleton5(); }
public static Singleton5 getInstance() { return SingletonHolder.instance; } }
|
该方法采用内部类来解决,加载一个类时,其内部类不会同时被加载。一个类被加载,当且仅当其某个静态成员(静态域、构造器、静态方法等)被调用时发生。
参考地址