人生在世,谁不面试。单例模式:一个搞懂不加分,不搞懂减分的知识点
又一篇一抓一大把的博文,但是你真的的搞懂了吗?点开看看。。过后,你也来一篇。java
单例模式是面试中很是喜欢问的了,咱们每每自认为已经彻底理解了,没什么问题了。但要把它手写出来的时候,可能出现各类小错误,下面是我总结的快速准确的写出单例模式的方法。git
单例模式有各类写法,什么「双重检锁法」、什么「饿汉式」、什么「饱汉式」,老是记不住、分不清。这就对了,人的记忆力是有限的,咱们应该记的是最基本的单例模式怎么写。github
单例模式:一个类有且只能有一个对象(实例)。单例模式的 3 个要点:面试
private Singleton(){}
public static Singleton getInstance()
private static Singleton instance
<!--more-->安全
类加载的时候就新建实例多线程
public class Singleton { private static Singleton instance = new Singleton(); private Singleton() { } public static Singleton getInstance() { return instance; } public void show(){ System.out.println("Singleon using static initialization in Java"); } } // Here is how to access this Singleton class Singleton.getInstance().show();
当执行 Singleton.getInstance() 时,类加载器加载 Singleton.class 进虚拟机,虚拟机在方法区(元数据区)为类变量分配一块内存,并赋值为空。再执行 <client>()
方法,新建实例指向类变量 instance。这个过程在类加载阶段执行,并由虚拟机保证线程安全。因此执行 getInstance() 前,实例就已经存在,因此 getInstance() 是线程安全的。并发
不少博文说 instance 还须要声明为 final,其实不用。final 的做用在于不可变,使引用 instance 不能指向另外一个实例,这里用不上。固然,加上也没问题。函数
<!--// final 修饰的基本数据类型,在编译期时,初始化数据放在常量池-->this
这个写法有一个不足之处,就是若是须要经过参数设置实例,则没法作到。举个栗子:
class Singleton { private static Singleton instance = new Singleton(); private Singleton() { } // 不能设置 name! public static Singleton getInstance(String name) { return instance; } public void show(){ System.out.println("Singleon using static initialization in Java"); } } // Here is how to access this Singleton class Singleton.getInstance(String name).show();
考虑到这种状况,就在调用 getInstance() 方法时,再新建实例。
public class Singleton { private static Singleton instance; private String name; private Singleton(String name) { this.name = name; } public static synchronized Singleton getInstance(String name) { if (instance == null) { instance = new Singleton(name); } return instance; } public String show() { return name; } } Singleton.getInstance(String name).show();
这里加了 synchronized
关键字,能保证只会生成一个实例,但效率不高。由于实例建立成功后,再获取实例时就不用加锁了。
当不加 synchronized 时,会发生什么:
instance 是类的变量,类存放在方法区(元数据区),元数据区线程共享,因此类变量 instance 线程共享,类变量也是在主内存中。线程执行 getInstance() 时,在本身工做内存新建一个栈帧,将主内存的 instance 拷贝到工做内存。多个线程并发访问时,都认为 instance == null
,就将新建多个实例,那单例模式就不是单例模式了。
实现只在建立的时候加锁,获取时不加锁。
public class Singleton { private static volatile Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) { synchronized(Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } }
为何要判断两次:
多个线程将 instance 拷贝进工做内存,即多个线程读取到 instance == null,虽然每次只有一个线程进入 synchronized 方法,当进入线程成功新建了实例,synchronized 保证了可见性(在 unlock 操做前将变量写回了主内存),此时 instance 不等于 null 了,但其余线程已经执行到 synchronized 这里了,某个线程就又会进入 synchronized 方法,若是不判断一次,又会再次新建一个实例。
为何要用 volatile 修饰 instance:
synchronized 能够实现原子性、可见性、有序性。其中实现原子性:一次只有一个线程执行同步块的代码。但计算机为了提高运行效率,会指令重排序。
代码 instance = new Singleton(); 会被拆为 3 步执行。
若是 instance 都在 synchronized 里面,那么没啥问题,问题出如今 instance 在 synchronized 外边,由于此时外边一群饿狼(线程),就在等待一个 instance 这块肉不为 null。
模拟一下指令重排序的出错场景:多线程环境下,正好一个线程,在同步块中按 ACB 执行,执行到 AC 时(并将 instance 写回了主内存),另外一个线程执行第一个判断时,认为 instance 不为空,返回 instance,但此时 instance 还没被正确初始化,因此出错。
当 instance 被 volatile 修饰时,只有 ACB 执行完了以后,其余线程才能读取 instance
为何 volatile 能禁止指令重排序:它在 ACB 后添加一个 lock 指令,lock 指令以前的操做执行完成后,后面的操做才能执行
你可能认为上面的解释太复杂,很差理解。对,确实比较复杂,我也搞了好久才搞明白。你能够看看这个是否是更好理解,Java 虚拟机规范的其中一条先行发生原则:对 volatile 修饰的变量,读操做,必须等写操做完成。
枚举写法:
public enum EasySingleton{ INSTANCE; }
当面试官让我写一个单例模式,我老是以为写这个好像有点另类
静态内部类写法:
public class Singleton { private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } private Singleton (){} public static final Singleton getInstance() { return SingletonHolder.INSTANCE; } }
单例模式主要为了节省内存开销,Spring 容器的 Bean 就是经过单例模式建立出来的。
单例模式没写出来,那也没啥事,由于那下一个问题你也不必定能答出来 :)。
本篇文章由一文多发平台ArtiPub自动发布