单例模式

  • 懒汉单例(线程不安全的)
public class Singleton {
    private Singleton(){}
    private static Singleton instance = null;
    public static Singleton getInstance(){
        //在第一次调用实例方法时才进行对象的实例化
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

线程不安全的方法,若有多个线程就会实例化很多对象

  • 懒汉单例的优化:在方法上加锁
public class Singleton {
    private Singleton(){}
    private static Singleton instance = null;
    public static synchronized Singleton getInstance(){
        if (instance == null){
            instance = new Singleton();
        }
        return instance;
    }
}

方法上加synchronized 使得每次只能有一个线程进入,虽然是线程安全的方法,但其他线性会被阻塞,效率很低

  • 锁同步代码块 ,双重检查
public class Singleton {
    private Singleton(){}
    private static Singleton instance = null;
    public static Singleton getInstance(){
        if (instance == null){
        synchronized(Singleton.class){
        if(instance == null){
         instance = new Singleton();
             }
           } 
        }
        return instance;
    }
}

这段代码看似非常完美,但是其实问题就在instance = new Singleton()这一句上,这句好不是原子的,执行这句代码,实际干了三件事:

  1. 给instance分配空间——即在堆上开辟一块空间
  2. 调用Singleton的构造函数初始化成员变量
  3. 栈上的引用指向堆内存
    但是在JVM编译中存在指令重排序的优化。也就是说,第二步和第三步的顺序是不能保证的。可能是1->2->3,也可能是1->3->2,如果是前者,一定会保证在第三步走完instance才不为空,如果是后者,若在第3步执行之前被线程2抢占了,那么此时instance!=null 了,初始化还没有完成,就会导致返回的对象是一个不完整的对象。
    双重检查加volatile
public class SafeSingleton {
    private volatile static SafeSingleton intance = null;
    private SafeSingleton(){}
    public static SafeSingleton getIntance(){
        if (intance == null){
            synchronized (SafeSingleton.class){
                if (intance == null){
                    intance = new SafeSingleton();
                }
            }
        }
        return intance;
    }
}

加上volatile关键字,禁止指令重排序
双重检查:第一层的作用提高性能,当有很多线程进入方法中时,只有一个线程可以真正的创建对象,若没有第一层叛空操作,这些线程在抢占锁之前不知道有一个线程已经创建好了对象,就会有很多无用的加锁解锁工作,若有了第一层叛空操作,在争抢锁之前就会知道已经有了对象,这时就不会进入同步代码块

  • 饿汉单例,线程安全
public class SingletonHungry {
    private SingletonHungry(){}
    //构造方法私有化,保证只能被实例化一次 
    public static SingletonHungry instance = new SingletonHungry();
    public static SingletonHungry getInstance(){
        return instance;
    }
}
  • 静态内部类中实例化对象
public class StaticInnerSingleton {
    private StaticInnerSingleton(){}
    private static class Inner{
        private static StaticInnerSingleton instance = new StaticInnerSingleton();
    }
    public static StaticInnerSingleton getInstance(){
        return Inner.instance;
    }
}

类在初始化时,JVM会获取本类Class对象的锁,保证多个线程时,类只会被初始化一次

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值