设计模式之单例模式(饿汉与懒汉)

什么是单例模式?

单例模式,属于创建类型的一种常用的软件设计模式。通过单例模式的方法创建的类在当前进程中只有一个实例(所以构造器要私有化)。

一:饿汉式

/**
 * 饿汉式
 */
public class Hungry_Man {

    private Hungry_Man (){ };

    private final static Hungry_Man HUNGRY_MAN = new Hungry_Man();

    private static Hungry_Man getHungryMan(){
        return HUNGRY_MAN;
    }

}

饿汉式上来就实例化了,非常的消耗内存,所有就有了懒汉式。

二:懒汉式

/**
 * 懒汉式
 */
public class LazyMan {

    private LazyMan() {};

    private static LazyMan lazyMan;

    private static LazyMan getLazyMan() {
        if (lazyMan == null) {
            lazyMan = new LazyMan();
        }
        return lazyMan;
    }
}

懒汉式只有在被调用的时候才会被实例化,比较节省内存,但是这种情况在单线程模式下是安全的,多线程下则有可能会创建多个对象。

多线程模式
/**
 * 懒汉式
 */
public class LazyMan {

    private LazyMan() {
        System.out.println(Thread.currentThread().getName());
    };

    private static LazyMan lazyMan;

    private static LazyMan getLazyMan() {
        if (lazyMan == null) {
            lazyMan = new LazyMan();
        }
        return lazyMan;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                LazyMan.getLazyMan();
            }).start();
        }
    }
}

​ 输出结果:

在这里插入图片描述在这里插入图片描述

所以在单模式下,可能会创建多个对象,这就违背了单例模式的原则,那怎样得到一个线程安全的的懒汉式呢?

DCL懒汉式
/**
 * 懒汉式
 */
public class LazyMan {
    private LazyMan() {
        System.out.println(Thread.currentThread().getName());
    };

    private static LazyMan lazyMan;

    private static LazyMan getLazyMan() {
        //双重检测锁 DCL懒汉式
        if (lazyMan==null){
            synchronized (LazyMan.class) {
                if (lazyMan == null) {
                    lazyMan = new LazyMan();
                }
            }
        }
        return lazyMan;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                LazyMan.getLazyMan();
            }).start();
        }
    }
}

但是这种情况下也是有可能不安全的的,因为在if语句里new的时候,表面上只有一部操作,实际上底层进行的三部操作

​ 1.开辟内存空间

​ 2.调用构造器,进行初始化

​ 3.把地址返回给对象

​ 按道理讲会123一步一步的执行,但是有可能会指令重排按照132这个顺序执行。

​ 第一个线程进来之后,执行了13,这时候第二线线程进来了就会以为对象已经被创建,就会执行return,但是第一个线程还没有进行第二步操作。

所以需要加上一个volatile(避免指令重排)

volatile
/**
 * 懒汉式
 */
public class LazyMan {
    private LazyMan() {
        System.out.println(Thread.currentThread().getName());
    };
        //volatile 避免指令被重排
    private volatile static LazyMan lazyMan;

    private static LazyMan getLazyMan() {
        //双重检测锁 DCL懒汉式
        if (lazyMan==null){
            synchronized (LazyMan.class) {
                if (lazyMan == null) {
                    lazyMan = new LazyMan();
                }
            }
        }
        return lazyMan;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                LazyMan.getLazyMan();
            }).start();
        }
    }
}

但是可以通过反射破解,枚举当我没说昂(这里就不讲反射怎么破解了,知道就行)。

三:内部类版本

public class Internal_lazyMan {

    private Internal_lazyMan(){};

    private static Internal_lazyMan getInstance(){
        return Internal.internal_lazyMan;
    }
   public static class  Internal{
        private  static Internal_lazyMan internal_lazyMan = new Internal_lazyMan();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值