单例模式
饿汉式单例
public class Hungry{
private Hungry(){} //构造器私有
private static Hungry HUNGRY = new Hungry();
public Hungry getInstance(){
return HUNGRY;
}
}
饿汉式单例的缺点是: 创建类时需要加载全部资源,可能会造成资源空间的浪费
懒汉式单例
public class LazyMan{
private LazyMan(){};
private static LazyMan lazyman;
public LazyMan getInstance(){
if(lazyman==null){ // 只有当lazyman为空时才创建对象
lazyman = new LazyMan();
}
}
}
在并发下,懒汉式单例创建对象破坏了单例的特性
public class LazyMan {
private LazyMan(){
System.out.println(Thread.currentThread().getName()+"Ok");
}
private static LazyMan lazyMan;
public static LazyMan getInstance(){
if(lazyMan==null){
lazyMan = new LazyMan(); //不是一个原子性操作
}
return lazyMan; // 此时lazyMan还没有完成构造
}
// 多线程
public static void main(String[] args) throws Exception {
for (int i = 1; i <=10 ; i++) {
new Thread(()->{
LazyMan.getInstance();
}).start();
}
}
}
双重检测锁模式的懒汉式单例解决-->DCL懒汉式单例
public class LazyMan {
private LazyMan(){
System.out.println(Thread.currentThread().getName()+"Ok");
}
private static LazyMan lazyMan;
// 双重检测模式的懒汉式单列 DCL懒汉式
public static LazyMan getInstance(){
if(lazyMan==null){
synchronized (LazyMan.class){
if(lazyMan==null){
lazyMan = new LazyMan();
}
}
}
return lazyMan;
}
public static void main(String[] args) throws Exception {
for (int i = 1; i <=10 ; i++) {
new Thread(()->{
LazyMan.getInstance();
}).start();
}
}
}
继续演讲,lazyMan = new LazyMan(); //不是一个原子操作, /** * 1. 分配内存空间 * 2, 执行构造方法 * 3. 把这个对象指向这个空间 */ 可能出现指令重排,因此使用volatile关键字禁止指令重排。
public class LazyMan {
private LazyMan(){
System.out.println(Thread.currentThread().getName()+"Ok");
}
private volatile static LazyMan lazyMan;
// 双重检测模式的懒汉式单列 DCL懒汉式 + 原子性操作
public static LazyMan getInstance(){
if(lazyMan==null){
synchronized (LazyMan.class){
if(lazyMan==null){
lazyMan = new LazyMan();
}
}
}
return lazyMan;
}
public static void main(String[] args) throws Exception {
for (int i = 1; i <=10 ; i++) {
new Thread(()->{
LazyMan.getInstance();
}).start();
}
}
}
静态内部类单例
public class Holder{
private Holder(){
}
public static Holder getInstance(){
return InnerClass.HOLDER;
}
public static class InnerClass{
private static final Holder HOLDER = new Holder();
}
public static void main(String[] args) throws Exception {
Holder instance = Holder.getInstance();
Holder instance1 = Holder.getInstance();
System.out.println(instance);
System.out.println(instance1);
}
}
上面这三种单例模式都存在一个问题就是可通过反射的方式去破坏它,这里使用静态内部类创建单例,使用反射破解来演示。
public class Holder {
private Holder(){}
public static Holder getInstance(){ return InnerClass.HOLDER;}
public static class InnerClass{ private static final Holder HOLDER = new Holder(); }
public static void main(String[] args) throws Exception {
Holder instance = Holder.getInstance();
Holder instance1 = Holder.getInstance();
Constructor<Holder> declaredConstructor = Holder.class.getDeclaredConstructor(null);
declaredConstructor.setAccessible(true);
Holder instance2 = declaredConstructor.newInstance();
System.out.println(instance);
System.out.println(instance1);
System.out.println(instance2); // 反射破坏了单例模式
}
}
枚举型单例
由于上面三种单例都能被反射破坏因此,使用枚举型单例
public enum EnumSingle{
INSTANCE;
public EnumSingle getInstance(){
return INSTANCE;
}
}
class Test{
public static void main(String[] args) throws Exception {
EnumSingle instance = EnumSingle.getInstance();
EnumSingle instance2 = EnumSingle.getInstance();
Constructor<EnumSingle> declaredConstructor = EnumSingle.class.
getDeclaredConstructor(String.class,int.class);//注意这里要加两个参数,通过对代码进行反编译,我们发现EnumSingle的构造方法是一个无参构造方法,但是不传递参数,这里会报一个异常,通过jad工具进行反编译发现EnumSingle其实有两个参数分别为String和in类型。
declaredConstructor.setAccessible(true);
EnumSingle instance3 = declaredConstructor.newInstance();
System.out.println(instance);
System.out.println(instance2);
System.out.println(instance3);
}
}
如果不传参数,抛出下面的异常:
学习内容来源: https://www.bilibili.com/video/BV1B7411L7tE?p=33&spm_id_from=pageDriver