A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© netboy032 中级黑马   /  2012-12-28 11:19  /  1586 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. class Singleton{
  2.       private Singleton(){}
  3.      
  4.       private static class SingletonContainer{
  5.             private static Singleton si = new Singleton();
  6.      }
  7.      
  8.       public static Singleton getSingleton(){
  9.             return SingletonContainer. si;
  10.      }
  11. }
复制代码
1.上面这个单例类会不会有多线程的问题?到底是懒汉式还是饿汉式?解释下
2.用多线程的方式解决了懒汉式的问题,我想问能不能用代码写出来,我说的是能运行的代码?

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

2 个回复

倒序浏览
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;  
    }  
同时更新某些属性的实例

评分

参与人数 1技术分 +1 收起 理由
冯海霞 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马