单例模式
来源:互联网 发布:房地产大数据 走势 编辑:程序博客网 时间:2024/05/21 22:31
public class Single{
private static Single singleInstance ;
注意1: 构造方法一定要私有的,谁也不能随便创造一个Single
pirvate Single(){
}
public static getSingleInstance (){
if(singleInstance == null) {
singleInstance = newSingle();
}
return singleInstance;
}
注意2: 在多线程情况下, 有可能会导致创建不止一个singleInstance,因为当线程同时进行到if(singleInstance == null) { 都为空,都去创建,
解决方法 :
方法1: public synchronize static getSingleInstance () 让线程同步,这种方法会导致性能损耗
方法2: 在初始化的时候 直接创建singleInstance
private static Single singleInstance =getSingleInstance (); //
方法3:只在第一次的时候同步,double check
public static getSingleInstance (){
if(singleInstance == null) {
synchronize(Single.class){
if(singleInstance == null) {
singleInstance = newSingle();
}
}
returnsingleInstance;
}
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- java 编程规范
- Struts2标签实现for循环 分页 选择第几页
- struts2 doubleselect 二级级联菜单
- 人生的枷锁是人生有意义
- 学习拾掇
- 单例模式
- eclipse 安装 svn findbugs
- 我要搬家
- 浅谈Oracle VM VirtualBox虚拟机的网络配置
- ollydbg和IDA,HIEW的简单结合
- C++标准库find简单用法
- my english document for threadx
- .NET 泛型
- 对container_of(ptr,type,member)分析