设计模式:单例模式

参考
JAVA设计模式之单例模式

单例模式

类型:创建型

确保一个类最多只有一个实例,并提供一个全局访问点
单例模式有以下特点:

  1. 单例类只能有一个实例。
  2. 单例类必须自己创建自己的唯一实例。
  3. 单例类必须给所有其他对象提供这一实例。

采取的措施:
通过将构造方法限定为private避免了类在外部被实例化(暂且当做没有反射机制)
在同一个虚拟机范围内,Singleton的唯一实例只能通过getInstance()方法访问

饿汉型

相对而言要考虑的较少,因为他的机制使他天生就是线程安全的,故放在第一个

package 单例模式.singleton;

public class PreloadSingleton {
    public static PreloadSingleton instance = new PreloadSingleton();

    //避免了类在外部被实例化
    private PreloadSingleton() { };

    public static PreloadSingleton getInstance() {
        return instance;
    }
}

懒汉型

版本1.0

最基本的样子,当第一次调用getInstance方法时才new实例

package 单例模式.singleton;

public class LazyLoadSingleton {
    private static LazyLoadSingleton instance=null;

    private LazyLoadSingleton(){ }
    
    public static LazyLoadSingleton getInstance() {
        if(instance==null) {
            instance=new LazyLoadSingleton();
        }
        return instance;
    }
}

但是它是线程不安全的,并发环境下很可能出现多个LazyLoadSingleton实例

版本2.0

使用线程同步,保证线程安全
但是,如果要经常的调用getInstance()方法,不管有没有初始化实例,都会唤醒和阻塞线程。

package 单例模式.singleton;

public class LazyLoadSingleton {
    private static LazyLoadSingleton instance=null;

    private LazyLoadSingleton(){ }

    public static synchronized LazyLoadSingleton getInstance() {
        if(instance==null) {
            instance=new LazyLoadSingleton();
        }
        return instance;
    }
}

版本3.0

把sychronized加在if(instance==null)判断语句里面,保证instance未实例化的时候才加锁

package 单例模式.singleton;

public class LazyLoadSingleton {
    private static LazyLoadSingleton instance=null;

    private LazyLoadSingleton(){ }

    public static synchronized LazyLoadSingleton getInstance() {
        if(instance==null) {
            synchronized (LazyLoadSingleton.class){
                if(instance==null) {
                    instance=new LazyLoadSingleton();
                }
            }
        }
        return instance;
    }
}

终极版本

new 一个对象是有代码执行顺序的,而并发环境下是无法保证顺序性的
因此,我们需要使用另一个关键字volatile保证对象实例化过程的顺序性。

memory=allocate();//1:初始化内存空间
ctorInstance(memory);//2:初始化对象
instance=memory();//3:设置instance指向刚分配的内存地址

package 单例模式.singleton;

public class LazyLoadSingleton {
    private static volatile LazyLoadSingleton instance=null;

    private LazyLoadSingleton(){ }

    public static synchronized LazyLoadSingleton getInstance() {
        if(instance==null) {
            synchronized (LazyLoadSingleton.class){
                if(instance==null) {
                    instance=new LazyLoadSingleton();
                }
            }
        }
        return instance;
    }
}

总结

线程安全:

饿汉式天生就是线程安全的,可以直接用于多线程而不会出现问题,

懒汉式本身是非线程安全的,为了实现线程安全有几种写法,这三种实现在资源加载和性能方面有些区别。

资源加载和性能:

饿汉式在类创建的同时就实例化一个静态对象出来,不管之后会不会使用这个单例,都会占据一定的内存,但是相应的,在第一次调用时速度也会更快,因为其资源已经初始化完成,

而懒汉式顾名思义,会延迟加载,在第一次使用该单例的时候才会实例化对象出来,第一次调用时要做初始化,如果要做的工作比较多,性能上会有些延迟,之后就和饿汉式一样了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值