package cn.tom.init;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Created by lenovo on 2017/2/22.
*/
public class CacheingEnumResolver {
//单态实例 一切问题皆由此行引起
private static final CacheingEnumResolver CACHEING_ENUM_RESOLVER = new CacheingEnumResolver();
private static Map<String, String> CODE_MAP_CACHE;
static {
CODE_MAP_CACHE = new HashMap<String, String>();
CODE_MAP_CACHE.put("0", "北京市");
}
private CacheingEnumResolver() {
initEnums();
}
public static void initEnums() {
if (CODE_MAP_CACHE == null) {
System.out.println("CODE_MAP_CACHE为空,问题在这里开始暴露");
CODE_MAP_CACHE = new HashMap<String, String>();
}
CODE_MAP_CACHE.put("1", "北京市");
CODE_MAP_CACHE.put("2", "河南省");
}
public Map getCache() {
return Collections.unmodifiableMap(CODE_MAP_CACHE);
}
public static CacheingEnumResolver getInstance() {
return CACHEING_ENUM_RESOLVER;
}
public static void main(String[] args) {
System.out.println(CacheingEnumResolver.getInstance().getCache());
}
}
饿汉式单例可能会引发的问题
最新推荐文章于 2024-02-05 12:35:28 发布