单例模式---懒汉式(用的时候去实例化)

来源:互联网 发布:淘宝课件 编辑:程序博客网 时间:2024/06/15 04:24


package cn.thcic;

/**
 * 懒汉式(用的时候去实例化)单例模式
 *
 * by Zhiwang Zhang on 2014年7月18日
 */
public class Test101 {
 // 私有的静态的本类属性
 private static Test101 test101;

 // 私有无参构造方法
 private Test101() {
 }

 // 公有的同步的(保证线程同步)静态的创建实例的方法
 public synchronized static Test101 getInstance() {
  if (test101 == null) {
   test101 = new Test101();
  }
  return test101;
 }
}

0 0