黑马程序员技术交流社区
标题:
关于单例类的问题
[打印本页]
作者:
netboy032
时间:
2012-12-28 11:19
标题:
关于单例类的问题
class Singleton{
private Singleton(){}
private static class SingletonContainer{
private static Singleton si = new Singleton();
}
public static Singleton getSingleton(){
return SingletonContainer. si;
}
}
复制代码
1.上面这个单例类会不会有多线程的问题?到底是懒汉式还是饿汉式?解释下
2.用多线程的方式解决了懒汉式的问题,我想问能不能用代码写出来,我说的是能运行的代码?
作者:
王少雷
时间:
2012-12-28 11:41
private static GlobalConfig instance = null;
private Vector properties = null;
private GlobalConfig() {
//Load configuration information from DB or file
//Set values for properties
}
private static synchronized void syncInit() {
if (instance == null) {
instance = new GlobalConfig();
}
}
public static GlobalConfig getInstance() {
if (instance == null) {
syncInit();
}
return instance;
}
public Vector getProperties() {
return properties;
}
同时更新某些属性的实例
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2