今天想在项目中写一个懒汉式单例,在网上搜索了半天,连一个正确的懒汉式单例都没找到,好在也是唤醒了我脑海中关于懒汉式单例的DCL写法的回忆。
public static Instance getInstance() {
if (instance != null) {
return instance;
}
synchronized(Instance.class) {
if (instance != null) {
return instance;
}
instance = new Instance();
return instance;
}
}


