用懒加载方式实现一个单列模式(注意:需要考虑多线程并发情况)

1、懒汉式-线程不安全

这个实现在多线程环境下是不安全的,如果多个线程能够同时进入 if (uniqueInstance == null) ,并且此时 uniqueInstance == null,那么会有多个线程执行 uniqueInstance = new Singleton(); 语句,这将导致实例化多次 uniqueInstance。 


// 懒汉式: 线程不安全
// 有延迟加载: 不是在类加载的时候就创建了,而是在调用newStance()的时候才会创建
public class Singleton {

    private static Singleton uniqueInstance;

    private Singleton(){

    }

    public static Singleton newInstance(){
        if(uniqueInstance == null)
            uniqueInstance = new Singleton();
        return uniqueInstance;
    }
}

 2、懒汉式-线程安全-性能不好

为了解决上面的问题,我们可以直接在newInstance()方法上面直接加上一把synchronized同步锁。那么在一个时间点只能有一个线程能够进入该方法,从而避免了实例化多次 uniqueInstance

但是当一个线程进入该方法之后,其它试图进入该方法的线程都必须等待,即使 uniqueInstance已经被实例化了。这会让线程阻塞时间过长,因此该方法有性能问题,不推荐使用。

public static synchronized Singleton newInstance(){//在上面的基础上加了synchronized
    if(uniqueInstance == null)
        uniqueInstance = new Singleton();
    return uniqueInstance;
}

3、饿汉式-线程安全-无延迟加载

饿汉式就是 : 采取直接实例化 uniqueInstance 的方式,这样就不会产生线程不安全问题。

这种方式比较常用,但容易产生垃圾对象(丢失了延迟实例化(lazy loading)带来的节约资源的好处)。

它基于 classloader机制避免了多线程的同步问题,不过,×××tance 在类装载时就实例化,虽然导致类装载的原因有很多种,在单例模式中大多数都是调用 getInstance 方法, 但是也不能确定有其他的方式(或者其他的静态方法)导致类装载,这时候初始化 ×××tance 显然没有达到 lazyloading 的效果。]

public class Singleton {

    // 急切的创建了uniqueInstance, 所以叫饿汉式
    private static Singleton uniqueInstance = new Singleton();

    private Singleton(){
    }

    public static Singleton newInstance(){
        return uniqueInstance;
    }

    // 瞎写一个静态方法。这里想说的是,如果我们只是要调用 Singleton.getStr(...),
    // 本来是不想要生成 Singleton 实例的,不过没办法,已经生成了
    public static String getStr(String str) {return "hello" + str;}
}

4、双重校验锁-线程安全

uniqueInstance 只需要被实例化一次,之后就可以直接使用了。加锁操作只需要对实例化那部分的代码进行,只有当uniqueInstance 没有被实例化时,才需要进行加锁。

双重校验锁先判断 uniqueInstance 是否已经被实例化,如果没有被实例化,那么才对实例化语句进行加锁。

 

// 双重加锁
public class Singleton {

    // 和饿汉模式相比,这边不需要先实例化出来
    // 注意这里的 volatile,使用 volatile 可以禁止 JVM 的指令重排,保证在多线程环境下也能正常运行
    private volatile static Singleton uniqueInstance;

    private Singleton() {
    }

    public static Singleton newInstance() {
        if (uniqueInstance == null) {
            synchronized (Singleton.class) {
                // 这一次判断也是必须的,不然会有并发问题
                if (uniqueInstance == null) {
                    uniqueInstance = new Singleton();
                }
            }
        }
        return uniqueInstance;
    }
}

 
 

 

 

 

 

//java单例模式-懒加载(保证线程安全性)  
public static class SingletonOptimizeLazy {  
    static SingletonOptimizeLazy instance = null;  
    public static SingletonOptimizeLazy getInstance() {  
      if (instance == null) {  
        createInstance();  
      }  
      return instance;  
    }  
    private synchronized static SingletonOptimizeLazy createInstance() {  
      if (instance == null) {  
        instance = new SingletonOptimizeLazy();  
      }  
      return instance;  
    }  
}  
//java单例模式-懒加载(保证线程安全性)
public static class SingletonOptimizeLazy {
    static SingletonOptimizeLazy instance = null;
    public static SingletonOptimizeLazy getInstance() {
      if (instance == null) {
        createInstance();
      }
      return instance;
    }
    private synchronized static SingletonOptimizeLazy createInstance() {
      if (instance == null) {
        instance = new SingletonOptimizeLazy();
      }
      return instance;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萤火的微亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值