09 单例模式

单例模式

饿汉式

//饿汉式单例
public class Hungry {

    //可能会浪费空间
    private byte[] data1 = new byte[1024];

    private Hungry(){

    }

    private static final Hungry HUNGRY = new Hungry();

    public static Hungry  getInstance(){
        return HUNGRY;
    }
}

最基础懒汉式单例模式

//懒汉式单例模式
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;
    }

    public static void main(String[] args) {
        //多线程下并发会出问题
        for (int i = 0; i < 20; i++) {

            new Thread(()->{
                LazyMan.getInstance();
            }).start();
        }
    }
}

解决多线程并发下的问题 DCL懒汉式

//懒汉式单例模式  
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();//不是原子性操作  要加上volatile 避免指令重排
                    /*
                    * 1.分配内存空间
                    * 2.执行构造器方法,初始换对象
                    * 3.把这个对象指向这个空间
                    * */
                }
            }
        }
        return lazyMan;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 20; 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 class LazyMan {

    private static boolean flag = false;

    private LazyMan(){

        synchronized (LazyMan.class){

            if (flag == false){
                flag = true;
            }else{
                throw new RuntimeException("不要试图用反射破坏异常");
            }
        }

        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();//不是原子性操作  要加上volatile 避免指令重排
                    /*
                    * 1.分配内存空间
                    * 2.执行构造器方法,初始换对象
                    * 3.把这个对象指向这个空间
                    * */
                }
            }
        }
        return lazyMan;
    }

    public static void main(String[] args) throws Exception {
        //反射破环单例模式
        LazyMan instance = LazyMan.getInstance();
        Constructor<LazyMan> declaredConstructor = LazyMan.class.getDeclaredConstructor();
        declaredConstructor.setAccessible(true);
        LazyMan instance2 = declaredConstructor.newInstance();

        System.out.println(instance);
        System.out.println(instance2);

    }
}

枚举

//枚举 本身也是一个Class 类
public enum EnumSingle {

    INSTANCE;
    public EnumSingle getInstance(){
        return INSTANCE;
    }
}

class Test{

    public static void main(String[] args) throws Exception{
        EnumSingle instance1 = EnumSingle.INSTANCE;

        Constructor<EnumSingle> declaredConstructor = EnumSingle.class.getDeclaredConstructor(String.class, int.class);
        declaredConstructor.setAccessible(true);
        EnumSingle instance2 = declaredConstructor.newInstance();

        System.out.println(instance1);
        System.out.println(instance2);
    }
}

枚举类型不能使用反射会报错
09 单例模式

09 单例模式

上一篇:一、STM32之最小stm32硬件系统的实现


下一篇:MATLAB的基识(整理)