单例模式的多种方式与选择

前言

当我们使用单例模式的时候,在实际需求场景中。单例模式可以给我们带来更好的资源优化。但是同时,他也带来了线程安全问题,下面使用了7中方式的单例模式,在实际场景中,我们往往不会简简单单的使用饿汉模式或者懒汉模式。

代码


public class Test {

    /*
    单例模式,懒汉式,线程安全
     */
    public static class Singleton {
        private final static Singleton INSTANCE =new Singleton();
        private Singleton() {
        }
        public static Singleton getInstance(){
            return INSTANCE;
        }
    }

    /*
    单例模式,饿汉式,线程下不安全
     */
    public static class  Singleton2 {
        private static Singleton2 INSTANCE = null;
        private Singleton2() {
        }
        public static Singleton2 getINSTANCE(){
            if (INSTANCE == null)
                INSTANCE = new Singleton2();
            return INSTANCE;
        }

    }

    /*
    单例模式,饿汉式,线程安全,多线程环境下效率不高
     */
    public static class Singleton3 {
        private static Singleton3 INSTANCE = null;
        private Singleton3() {
        }
        private static synchronized Singleton3 getINSTANCE(){
            if (INSTANCE == null)
                INSTANCE = new Singleton3();
            return  INSTANCE;
        }
    }

    /*
    单例模式,懒汉式,变种,线程安全
     */
    public static class Singleton4 {
        private static Singleton4 INSTANCE = null;
        static {
            INSTANCE = new Singleton4();
        }
        private Singleton4(){
        }
        public static Singleton4 getINSTANCE() {
            return INSTANCE;
        }
    }

    /*
    单例模式,使用静态内部类,线程安全【推荐】
     */
    public static class Singleton5 {
        private final static class SingletonHolder {
            private static final Singleton5 INSTANCE = new Singleton5();
        }
        private Singleton5(){
        }
        public static Singleton5 getINSTANCE() {
            return SingletonHolder.INSTANCE;
        }
    }

    /*
    静态内部类,使用枚举方式,线程安全【推荐】
     */
    public enum Singleton6 {
        INSTANCE;
        public void whateverMethod() {
        }
    }

    /*
    静态内部类,使用双重校验锁,线程安全【推荐】
     */
    public static class Singleton7 {
        private volatile static Singleton7 INSTANCE = null;
        private Singleton7() {
        }
        public static Singleton7 getINSTANCE() {
            if (INSTANCE == null) {
                synchronized (Singleton7.class) {
                    if (INSTANCE == null)
                        INSTANCE = new Singleton7();
                }
            }
            return INSTANCE;
        }
    }

    /*
    测试
     */
    public static void main(String[] args) {
        System.out.println(Singleton.getInstance() == Singleton.getInstance());
        System.out.println(Singleton2.getINSTANCE() == Singleton2.getINSTANCE());
        System.out.println(Singleton3.getINSTANCE() == Singleton3.getINSTANCE());
        System.out.println(Singleton4.getINSTANCE() == Singleton4.getINSTANCE());
        System.out.println(Singleton5.getINSTANCE() == Singleton5.getINSTANCE());
        System.out.println(Singleton6.INSTANCE == Singleton6.INSTANCE);
        System.out.println(Singleton7.getINSTANCE() == Singleton7.getINSTANCE());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章