ksnowlv

回顾过去,总结以往;立足现在,铭记当下;技术为主,笔记而已.

Java/Android双重锁定模式

| Comments

双重检查锁定模式(DCLP)是一种软件设计模式,用来减少并发系统中竞争和同步的开销.

适用性方面:在J2SE 5.0之前的版本使用,有隐患。在J2SE 5.0开始的版本,可以放心使用,已经解决了原有的问题。可参考链接双重检查锁定模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 private volatile static ImageLoader instance;

  /** Returns singleton class instance */
  public static ImageLoader getInstance() {
      if (instance == null) {
          synchronized (ImageLoader.class) {
              if (instance == null) {
                  instance = new ImageLoader();
              }
          }
      }
      return instance;
  }

  //构造函数私有
  protected ImageLoader() {
  }
  

Comments

comments powered by Disqus
Included file 'custom/after_footer.html' not found in _includes directory