单例模式懒汉式Java版

要注意的都加注释了

package test;

/**
 * @Author 小浩
 * @Date 2020/4/12 15:36
 * @Version 1.0
 **/
public class Singleton {
    //  volatile 防止指令重排序 new对象不是原子操作
    private static volatile Singleton singleton;

    //  判断是否已经存在 防止反射修改构造方法为public
    private Singleton() {
        if (singleton != null) {
            try {
                throw new Exception("非法访问");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static Singleton getInstance() {
//        synchronized 不放在方法上提高效率 这样只在对象没生成的时候加锁
//        双重检测第二个检测是因为 有可能同时多个线程走进第一个if里边 再次判断可防止重复new对象
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
}

测试类:

通过反射修改构造方法为public 并且new对象发现抛异常,这是因为我们对非法访问进行了限制

package test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;


public class Test {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Class singletonClass = Singleton.class;

        Constructor[] declaredConstructors = singletonClass.getDeclaredConstructors();
        declaredConstructors[0].setAccessible(true);
        try {
            declaredConstructors[0].newInstance();

        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        
    }
}

输出结果:

单例模式懒汉式Java版_第1张图片

你可能感兴趣的:(JAVA)