- 双检索实现的单例,是线程安全的。
public class Singleleton {
private Singleleton() {}
private static Singleleton instance = null;
public static Singleleton getInstance() {
if(instance == null){
synchronize(Singleleton.class){
if(instance == null){
instance = new Singleton();
}
}
}
return instance;
}
}
- 枚举类型实现的单例,目前比较推荐
网友评论