本文共 4099 字,大约阅读时间需要 13 分钟。
饿汉式 和 DCL懒汉式
饿汉式
package com.chao.single;//饿汉式单例public class Hungry { //可能会浪费空间 private byte[] data1 = new byte[1024*1024]; private byte[] data2 = new byte[1024*1024]; private byte[] data3 = new byte[1024*1024]; private byte[] data4 = new byte[1024*1024]; //构造器私有,别人无法new,保持内存中只有一个对象 private Hungry(){ } //new 一个对象 private final static Hungry HUNGRY = new Hungry(); public static Hungry getInstance(){ return HUNGRY; }}
DCL 懒汉式
package com.chao.single;import java.lang.reflect.Constructor;import java.lang.reflect.Field;//懒汉式单例//道高一尺,魔高一丈!public class LazyMan { private static boolean chao = false; private LazyMan(){ synchronized (LazyMan.class){ if(chao == false){ chao = true; }else{ throw new RuntimeException("不要试图使用反射破坏异常"); } } } private volatile static LazyMan lazyMan; //双重检测锁模式的 懒汉式单例 DCL懒汉式 public static LazyMan getInstance(){ if(lazyMan==null){ synchronized (LazyMan.class){ if(lazyMan==null){ lazyMan = new LazyMan(); //不是一个原子性操作 } } } return lazyMan; } /** * 1. 分配内存空间 * 2. 执行构造方法,初始化对象 * 3. 把这个对象指向这个空间 * *123 * 132 A * B //此时lazyMan还没有完成构造 */ //单线程下确实单例OK //多线程并发 //反射! public static void main(String[] args) throws Exception {// for (int i = 0; i <10 ; i++) {// new Thread(()->{// LazyMan.getInstance();// }).start();//// }// LazyMan instance = LazyMan.getInstance(); Field chao = LazyMan.class.getDeclaredField("chao"); chao.setAccessible(true); ConstructordeclaredConstructor = LazyMan.class.getDeclaredConstructor(null); declaredConstructor.setAccessible(true); LazyMan instance = declaredConstructor.newInstance(); chao.set(instance,false); LazyMan instance2 = declaredConstructor.newInstance(); System.out.println(instance); System.out.println(instance2); }}
静态内部类
package com.chao.single;//静态内部类public class Holder { private Holder(){ } public static Holder getInstance(){ return InnerClass.HOLDER; } public static class InnerClass{ private static final Holder HOLDER = new Holder(); }}
单例不安全,反射
枚举
package com.chao.single;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;//enum 是一个什么?本身也是一个Class类public enum EnumSingle { INSTANCE; public EnumSingle getInstance(){ return INSTANCE; }}class Test{ public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException { EnumSingle instance1 = EnumSingle.INSTANCE; ConstructordeclaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class,int.class); declaredConstructor.setAccessible(true); EnumSingle instance2 = declaredConstructor.newInstance(); //java.lang.NoSuchMethodException: com.chao.single.EnumSingle. () System.out.println(instance1); System.out.println(instance2); }}
反编译 jad
枚举类型的最终反编译 源码:
// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.// Jad home page: http://www.kpdus.com/jad.html// Decompiler options: packimports(3) // Source File Name: EnumSingle.javapackage com.chao.single;public final class EnumSingle extends Enum{ public static EnumSingle[] values() { return (EnumSingle[])$VALUES.clone(); } public static EnumSingle valueOf(String name) { return (EnumSingle)Enum.valueOf(com/chao/single/EnumSingle, name); } private EnumSingle(String s, int i) { super(s, i); } public EnumSingle getInstance() { return INSTANCE; } public static final EnumSingle INSTANCE; private static final EnumSingle $VALUES[]; static { INSTANCE = new EnumSingle("INSTANCE", 0); $VALUES = (new EnumSingle[] { INSTANCE }); }}
转载地址:http://igrvz.baihongyu.com/