设计模式之单例模式(java实现)

单例模式的定义

单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

饿汉模式-线程安全

最简单的单例模式实现,在类加载时就是实例化对象
优点: 线程安全,单例模式的线程不安全是实例化唯一对象导致的。
缺点: 丢失了因为延时实例化而节省的资源的优点,不够环保。

public class Singleton {
    private static Singleton instance = new Singleton();
    private void Singleton(){}
    public static Singleton getInstance(){
        return instance;
    }
}

懒汉模式-线程不安全

延迟实现实例。
优点: 私有静态变量 被延迟实例化,这样做的好处是,如果没有用到该类,那么就不会实例化 ,从而节约资源。比较环保
缺点: 线程不安全,在多线程的情况下,可能多个线程进入方法体对私有静态变量实例化。

public class Singleton {
    private static Singleton instance;
    private void Singleton(){}
    public static Singleton getInstance(){
        if(instance == null){
                    instance = new Singleton();
                }
        return instance;
    }
 }

懒汉模式-线程安全

对访问的方法体用synchronized 进行同步,这样在同一时间就只有一个线程进入方法体,避免对私有静态变量重复实例化。

缺点:有性能问题, 当一个线程进入方法后,其他线程都会被阻塞,这时即使instance已经被实例化,其他线程也无法访问。

public class Singleton {
    private static Singleton instance;
    private void Singleton(){ }
    public static synchronized Singleton getInstance(){
        if(instance == null){
                    instance = new Singleton();
        }
        return instance;
    }

懒汉模式-双重校验锁

对线程安全的代码进一步优化,对实例化instance 的代码加锁

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

有的同学可能会问为什么不只对实例化instance 的代码加上锁,比如下述代码

if(intance == null){
	synchronized(Singleton.class){
                    instance = new Singleton();
            }
 }

大家可以设想一下这时候,在instance = null的情况下,有两个线程进入里方法体中,这时候一个线程进入上锁代码块,执行instance = new Singleton();语句,另外一个线程会被阻塞,可当当前线程实例化instance后,另一个线程也会对实例化instance.

懒汉模式-双重校验锁(完全版)

Instance 采用 volatile 关键字修饰也是很有必要的, instance = new Singleton(); 这段代码其实是分为三步执行:

  1. 为instance 分配内存空间。
  2. 初始化instnce
  3. 将内存空间指向instance

但是由于 JVM 具有指令重排的特性,执行顺序有可能变成 1>3>2。指令重排在单线程环境下不会出现问题,但是在多线程环境下会导致一个线程获得还没有初始化的实例。例如,线程 T1 执行了 1 和 3,此时 T2 调用 getInstance() 后发现 instance 不为空,因此返回instance,但此时 instance 还未被初始化。

public class Singleton {
    private static volatile Singleton instance;
    private void Singleton(){ }
    public static Singleton getInstance(){
        if(instance == null){
            synchronized(Singleton.class){
                if(instance == null){
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }

后记
其实单例模式还可以用其他方法实现,比如枚举和静态内部类实现
不过这些已经够应付面试了。我就懒得记录了╮(╯▽╰)╭

谢谢阅读!

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值