延迟加载的单例设计模式: 不就是懒汉式吗??
- class Single
- {
- private static Single s = null;
- private Single(){}
- public static Single getInstance()
- {
- if(s==null)
- s = new Single();
- return s;
- }
- }
复制代码
- class Single
- {
- private static Single s = null;
- private Single(){}
- public static Single getInstance()
- {
- if(s==null)
- {
- synchronized(Single.class) //懒汉式多线程容易出问题 ,加双重判断解决了这个问题
- {
- if(s==null)
- s = new Single();
- }
- }
- return s;
- }
- }
复制代码
|