复习了一遍 Singleton 的 5 种写法

2015 年 7 月 16 日
 OpooPages

https://gist.github.com/opoo/e48dbc0b3c5d0afddfc0

好吧,github gist 貌似暂时不能访问了,贴在下面。

public class SingletonDemo {
    public static class Singleton1{
        private static final Singleton1 instance = new Singleton1();
        private Singleton1(){}
        public static Singleton1 getInstance(){
            return instance;
        }
    }

    public static class Singleton2{
        private static Singleton2 instance;
        private Singleton2(){};
        public static synchronized Singleton2 getInstance(){
            if(instance == null){
                instance = new Singleton2();
            }
            return instance;
        }
    }

    public static class Singleton3{
        private static volatile Singleton3 instance;
        private Singleton3(){}
        public static Singleton3 getInstance(){
            if(instance == null){
                synchronized (Singleton3.class){
                    if(instance == null){
                        instance = new Singleton3();
                    }
                }
            }
            return instance;
        }
    }

    public static class Singleton4{
        private static final Singleton4 instance;
        static{
            instance = new Singleton4();
        }
        private Singleton4(){}
        public static Singleton4 getInstance(){
            return instance;
        }
    }

    public static class Singleton5{
        private static class SingletonHolder{
            private static final Singleton5 instance = new Singleton5();
        }
        private Singleton5(){}
        public static Singleton5 getInstance(){
            return SingletonHolder.instance;
        }
    }
}
5088 次点击
所在节点    Java
17 条回复
cikelengfeng
2015 年 7 月 16 日
hey boy:

public enum MySingleton {
INSTANCE;
}
yxaaa123
2015 年 7 月 16 日
比孔乙己知道得多
delavior
2015 年 7 月 16 日
茴字有四种写法,你知道吗
slixurd
2015 年 7 月 16 日
没有Enum版,差评
感觉只要知道一个double-check Locking版本,一个enum就够了。
FrankFang128
2015 年 7 月 16 日
var s = {}
成功
anohana
2015 年 7 月 16 日
都是非线程安全的
OpooPages
2015 年 7 月 16 日
@cikelengfeng
@slixurd
好吧,enum方式也挺好。

个人比较喜欢 double-checked locking和 SingletonHolder 的。

@anohana 好像是线程安全的吧,不然要什么 synchronized、volatile干啥。
zddhub
2015 年 7 月 16 日
当年面试,问我怎么继承单例,直接懵逼了。
OpooPages
2015 年 7 月 16 日
@zddhub 求指教,我也不知道怎么继承单例。
hitsmaxft
2015 年 7 月 16 日
@anohana 最后一个是依赖 classloader 实现, 所以是线程安全的。
aliuwr
2015 年 7 月 16 日
@hitsmaxft 4 和 5 相比有什么区别?
swolf119
2015 年 7 月 16 日
嘻嘻,反射调你私构函数,让你单!
evilgod528
2015 年 7 月 16 日
需要记那么多吗?记住 double-check Locking 还有 enum 就行了吧
hitsmaxft
2015 年 7 月 16 日
@aliuwr 没啥区别,就是innerclass理论上可以到getinstance调用时实例化,而不是利用static实例化。

但是,服务器一般希望启动时全部依赖初始化完毕,否者运行到这里才挂起去加载类,鬼知道哪天就出问题了。。
bgcolor0325
2015 年 7 月 17 日
线程不安全啊
caixiexin
2015 年 7 月 23 日
说来惭愧,最近也才知道enum这种简单安全的写法。。
tchekai704
2015 年 8 月 31 日
double-checked locking 似乎和 wiki 上的不一样吧
https://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/206135

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX