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;
}
}
}
1
cikelengfeng 2015-07-16 17:22:59 +08:00
hey boy:
public enum MySingleton { INSTANCE; } |
2
yxaaa123 2015-07-16 18:14:46 +08:00
比孔乙己知道得多
|
3
delavior 2015-07-16 18:40:39 +08:00
茴字有四种写法,你知道吗
|
4
slixurd 2015-07-16 18:45:45 +08:00
没有Enum版,差评
感觉只要知道一个double-check Locking版本,一个enum就够了。 |
5
FrankFang128 2015-07-16 18:57:46 +08:00 via Android
var s = {}
成功 |
6
anohana 2015-07-16 18:59:41 +08:00 via iPhone
都是非线程安全的
|
7
OpooPages OP @cikelengfeng
@slixurd 好吧,enum方式也挺好。 个人比较喜欢 double-checked locking和 SingletonHolder 的。 @anohana 好像是线程安全的吧,不然要什么 synchronized、volatile干啥。 |
8
zddhub 2015-07-16 19:52:21 +08:00 via iPhone
当年面试,问我怎么继承单例,直接懵逼了。
|
12
swolf119 2015-07-16 22:28:58 +08:00
嘻嘻,反射调你私构函数,让你单!
|
13
evilgod528 2015-07-16 22:30:31 +08:00
需要记那么多吗?记住 double-check Locking 还有 enum 就行了吧
|
14
hitsmaxft 2015-07-16 22:35:02 +08:00 via iPhone 1
@aliuwr 没啥区别,就是innerclass理论上可以到getinstance调用时实例化,而不是利用static实例化。
但是,服务器一般希望启动时全部依赖初始化完毕,否者运行到这里才挂起去加载类,鬼知道哪天就出问题了。。 |
15
bgcolor0325 2015-07-17 16:39:16 +08:00
线程不安全啊
|
16
caixiexin 2015-07-23 12:20:10 +08:00
说来惭愧,最近也才知道enum这种简单安全的写法。。
|
17
tchekai704 2015-08-31 19:44:27 +08:00
double-checked locking 似乎和 wiki 上的不一样吧
https://en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java |