单例模式之懒汉模式的线程安全解决方法
2015-10-15 15:07
295 查看
加同步代码块解决懒汉模式的线程安全问题,略显低效,用双重判断可以解决该问题
/**
* 懒汉式
* Created by Xu ZiYang on 2015/10/15.
*/
public class Singleton {
private static Singleton s = null;
private Singleton() {
}
public static Singleton getSingleton() {
if (s == null)
synchronized (Singleton.class){
if(s == null)
s = new Singleton();
}
return s;
}
}
/**
* 懒汉式
* Created by Xu ZiYang on 2015/10/15.
*/
public class Singleton {
private static Singleton s = null;
private Singleton() {
}
public static Singleton getSingleton() {
if (s == null)
synchronized (Singleton.class){
if(s == null)
s = new Singleton();
}
return s;
}
}
相关文章推荐
- Junit 2
- 你可能不知道的30个Python语言的特点技巧(1)
- 阿里巴巴中的FastJson解析
- Java性能优化
- CSS的压缩 方法与解压
- python的编码问题
- 微信硬件平台开发前。。。
- 指针设计类和对象回应程序
- Mac 设置环境变量 查看和添加PATH环境变量
- wxhl iOS bj 49 张浩 学习心得文章连载 第14章
- 近期感悟
- 上线需要注意的
- UML类图实例
- Android日志收集功能设计和实施报告(总)
- Android下各个按键对应的key code
- 在使用vs2012中的MVC4出的错误!类型“System.Data.Entity.DbContext”在未被引用的程序集中定义
- QComboBox下拉框下拉选项高度设置
- [sicily]1765. 笨小猴
- Win8.1系统打开VHD文件提示“你要如何打开此类型的文件”的解决方法
- eclipse的几个主要插件