首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

单例模式(懒汉形式)

2012-07-23 
单例模式(懒汉模式)public class Singleton {/*** 对保存实例的变量添加volatile的修饰*/private volatile

单例模式(懒汉模式)

public class Singleton {      /**      * 对保存实例的变量添加volatile的修饰      */      private volatile static Singleton instance = null;      private Singleton(){          }      public static  Singleton getInstance(){          //先检查实例是否存在,如果不存在才进入下面的同步块          if(instance == null){              //同步块,线程安全的创建实例              synchronized(Singleton.class){                  //再次检查实例是否存在,如果不存在才真的创建实例                  if(instance == null){                      instance = new Singleton();                  }              }          }          return instance;      }  } 
?

热点排行