单例实现汇总

1. 启动时加载
1.1 - static new
1.2 - static class new
2. 延迟加载
2.1 - synchronized
2.2 - 双重锁检查 + volatile
2.3 - Java9中引入AtomicReference
3. 枚举实现

public class SingletonDemo {

    //1. 启动时加载
    //      1.1 - static new
    //      1.2 - static class new
    //2. 延迟加载
    //      2.1 - synchronized
    //      2.2 - 双重锁检查 + volatile
    //      2.3 - Java9中引入AtomicReference
    //3. 枚举实现

    public static void main(String[] args) {
        System.out.println(SingletonV11.getInstance() == SingletonV11.getInstance());
        System.out.println(SingletonV12.getInstance() == SingletonV12.getInstance());
        System.out.println(SingletonSynchronized.getInstance() == SingletonSynchronized.getInstance());
        System.out.println(SingletonDoubleCheckVolatile.getInstance() == SingletonDoubleCheckVolatile.getInstance());
        System.out.println(SingletonAtomicReference.getInstance() == SingletonAtomicReference.getInstance());
        System.out.println(SingletonEnum.getInstance() == SingletonEnum.getInstance());
    }

    //1.1 - 启动时初始化单例
    static class SingletonV11 {
        private static final SingletonV11 singletonV11 = new SingletonV11();

        private SingletonV11() {
            System.out.println("SingletonV11()");
        }

        public static SingletonV11 getInstance() {
            return singletonV11;
        }
    }

    //1.2 - 启动时利用内部静态类初始化单例
    static class SingletonV12 {

        private static class SingletonHolder {
            private static final SingletonV12 singletonV12 = new SingletonV12();
        }

        private SingletonV12() {
            System.out.println("SingletonV12()");
        }

        public static SingletonV12 getInstance() {
            return SingletonHolder.singletonV12;
        }
    }

    //2.1 - synchronized版本,线程排队执行
    static class SingletonSynchronized {

        private static SingletonSynchronized singletonSynchronized = null;

        private SingletonSynchronized() {
            System.out.println("SingletonSynchronized()");
        }

        public synchronized static SingletonSynchronized getInstance() {
            if (singletonSynchronized == null) {
                singletonSynchronized = new SingletonSynchronized();
            }
            return singletonSynchronized;
        }
    }

    //2.2 - 双重检查 + volatile
    static class SingletonDoubleCheckVolatile {

        private volatile static SingletonDoubleCheckVolatile singletonDoubleCheckVolatile = null;

        private SingletonDoubleCheckVolatile() {
            System.out.println("耗时长");
            System.out.println("SingletonDoubleCheckVolatile()");
        }

        public static SingletonDoubleCheckVolatile getInstance() {
            if (singletonDoubleCheckVolatile == null) {
                synchronized(SingletonDoubleCheckVolatile.class) {
                    if (singletonDoubleCheckVolatile == null) {
                        //调用构造函数可能会存在指令重排,使用volatile
                        singletonDoubleCheckVolatile = new SingletonDoubleCheckVolatile();
                    }
                }
            }
            return singletonDoubleCheckVolatile;
        }
    }

    //2.3 - Java9中使用AtomicReference,避免volatile使得指令的上下部分会指令重排
    static class SingletonAtomicReference {

        private static AtomicReference<SingletonAtomicReference> singletonAtomicReference = new AtomicReference<>();

        private SingletonAtomicReference() {
            System.out.println("耗时长");
            System.out.println("SingletonAtomicReference()");
        }

        public static SingletonAtomicReference getInstance() {
            var localRef = singletonAtomicReference.getAcquire();
            if (localRef == null) {
                synchronized (SingletonAtomicReference.class) {
                    localRef = singletonAtomicReference.getAcquire();
                    if (localRef == null) {
                        singletonAtomicReference.setRelease(new SingletonAtomicReference());
                        localRef = singletonAtomicReference.getAcquire();
                    }
                }
            }
            return localRef;
        }
    }

    //3. 枚举实现,避免使用反射机制构造其他实例
    static enum SingletonEnum {
        INSTANCE;

        public static SingletonEnum getInstance() {
            return INSTANCE;
        }
    }
}

 

posted on 2022-05-16 07:17  -赶鸭子上架-  阅读(19)  评论(0编辑  收藏  举报