单利模式最安全高效写法

public class TestSingleton {
  private static TestSingleton instance;
  private TestSingleton() {}


  public static TestSingleton getInsatance() {
  if(instance == null){
    //TestSingleton.class锁住的是字节码文件
    synchronized (TestSingleton.class) {
      if(instance == null){
        instance = new TestSingleton();
      }
    }
  }
  return instance;
}

 

posted @ 2015-10-20 16:12  牛肉羊杂汤  阅读(179)  评论(0编辑  收藏  举报