线程(7),单例模式(饿汉模式,懒汉模式)

本文深入探讨了单例模式的两种实现方式:饿汉模式和懒汉模式,详细解析了它们的工作原理,特别是在线程安全性和效率上的差异及优化方法。
  • 在代码中有些概念,不应该存在多个实例,我们此时就应该用单列模式来解决

我们通过单列模式来解决的有些问题,保证指定的类只有一个实例(如果尝试创建多个实例,直接编译就会报错)
单例模式中有两种典型的实现方式
1.饿汉模式

public class Test14_Singleton2 {
    //懒汉模式

    static class  Singleton {
        //设为private 在内外部就无法new在这个类了
        private Singleton() {}
        
        //唯一实例
        private static Singleton instance = new Singleton();
        
        //只能得到,不能有set方法
        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);
    }
}

结果

true
  • 这就证明了我们只有唯一一个实例,(用==比较的是类的地址)

2 .饿汉模式

public class Test14_Singleton2 {
    //懒汉模式
    
    static class Singleton {
        private Singleton () {
            
        }
        
        private static Singleton instance = null;
        
        public static Singleton getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
}

懒汉模式与饿汉模式的唯一区别就是,懒汉模式没有直接实例化instance,而是在get方法中来判断是否为空后再去实例化。

  • 这个代码中,懒汉模式是线程不安全的,可能会出现一边读一边赋值的的可能性,会导致线程不安全,所以我们需要加锁以此来保证线程安全
public class Test14_Singleton2 {
    //懒汉模式

    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;
        }
    }
}
  • 但是可能出现先加锁的线程在修改,后加锁的线程在读取,导致的内存可见性问题,我们可以在 instance前加上 volatile 来保证线程安全
public class Test14_Singleton2 {
    //懒汉模式

    static class Singleton {
        private Singleton () {

        }

        private static volatile Singleton instance = null;

        public static Singleton getInstance() {
            synchronized (Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
            return instance;
        }
    }
}
  • 但是线程的效率可能还会很低,我们如何去提升效率呢,我们可以在加一个if循环,先判断intance是否为空,若是不为空就直接返回了,为空的话再加锁,然后判空,然后实例化。就不用每次进去直接加锁了,提升了效率
public class Test14_Singleton2 {
    //懒汉模式

    static class Singleton {
        private Singleton () {

        }

        private static volatile Singleton instance = null;

        public static Singleton getInstance() {
            if (instance == null) {
                synchronized (Singleton.class) {
                    if (instance == null) {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值