13. 多线程案例(1)——单例模式(饿汉模式/懒汉模式)

1.为什么要使用单例模式

通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。如果希望在系统中某个类的对象只能存在一个,单例模式是最好的解决方案

对于系统中的某些类来说,只有一个实例很重要,例如,一个系统中可以存在多个打印任务,但是只能有一个正在工作的任务。如果不使用机制对窗口对象进行唯一化,将弹出多个窗口,如果这些窗口显示的内容完全一致,则是重复对象,浪费内存资源;如果这些窗口显示的内容不一致,则意味着在某一瞬间系统有多个状态,与实际不符,也会给用户带来误解,不知道哪一个才是真实的状态。因此有时确保系统中某个对象的唯一性即一个类只能有一个实例非常重要。

2.饿汉模式

只要类被加载了,实例就会立即创建(实例创建时机比较早,并且只会创建一个实例)

类加载只有一次机会,不可能并发执行

对于饿汉模式,多线程调用getInstance()方法,由于getInstance()里只做了一件事:读取instance实例的地址,也就是可以多个线程同时读取一个变量——线程安全


public class ThreadDemo19 {
    //先创建一个表示单例的类
    //饿汉模式的单例实现
    static class Singleton{
        //把构造方法变成私有的,此时在该类外部就无法new这个类的实例
        private Singleton(){

        }
        //在来创建一个static的成员,表示Singleton类的唯一实例
        //static和类相关,和实例无关,类在内存中只有一份,static成员也就只有一份
        private static Singleton instance = new Singleton();
        public static Singleton getInstance(){
            return instance;
        }
    }

    public static void main(String[] args) {
        Singleton s = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s == s2);
    }
}

此时在主函数中, Singleton.getInstance()是获取该实例的唯一方式,不能是用其他方式获取实例了

在这里插入图片描述

3.懒汉模式

当类被加载的时候不会立刻实例化,而是真正使用这个实例的时候才会实例化(实例随用随创建,并且只会创建一个实例),效率比饿汉模式要高。

对于懒汉模式来说,getInstance()中做了三件事:
1.读取instance内容并判断
2.如果instance为null,就new实例(相当于修改)
3.返回实例的地址

在多线程运行时,实例化之前就是线程不安全的

public class ThreadDemo20 {
    static class Singleton{
        private Singleton(){

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

    public static void main(String[] args) {
        Singleton s = Singleton.getInstance();
        Singleton s2 = Singleton.getInstance();
        System.out.println(s == s2);
    }
}

在这里插入图片描述

3.懒汉模式——线程安全版本(低效率)

 static class Singleton{
        private Singleton(){

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

两个线程画图分析
在这里插入图片描述

4.懒汉模式——线程安全版本(高效率)

 static 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;
        }
    }

4.懒汉模式——线程安全版本(保证内存可见性)

要在instance前面加上volatile保证可见性

 static class Singleton{
        private Singleton(){

        }
        private volatile static Singleton instance = null;
        public static Singleton getInstance() {
            if (instance == null) {
                synchronized (Singleton.class) {
                    if (instance == null) {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值