栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java——》谈谈你对单例模式的理解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java——》谈谈你对单例模式的理解

一、单例模式的作用

单例模式的核心是保证一个类只有一个实例,并且提供一个访问实例的全局访问点。

二、单例模式的实现
实现方式优缺点
饿汉式线程安全,调用效率高 ,但是不能延迟加载,可能造成空间资源浪费
懒汉式:锁式线程安全,调用效率不高synchronized,能延迟加载
懒汉式:双重检测锁式在懒汉式的基础上解决并发问题
静态内部类式线程安全,资源利用率高,可以延时加载
枚举单例线程安全,调用效率高,但是不能延迟加载
1、饿汉式

public class SingletonForHungry {

    // 缺点:如果对象没有被使用,造成空间资源浪费
    private byte[] b1 = new byte[1024 * 1024];
    private byte[] b2 = new byte[1024 * 1024];
    private byte[] b3 = new byte[1024 * 1024];

    // 声明此类型的变量,并实例化,当该类被加载的时候就完成了实例化并保存在了内存中
    private static final SingletonForHungry singletonInstance = new SingletonForHungry();

    // 私有化所有的构造方法,防止直接通过new关键字实例化
    private SingletonForHungry() {
    }

    // 对外提供一个获取实例的静态方法
    public static SingletonForHungry getSingletonInstance() {
        return singletonInstance;
    }

    public static void main(String[] args) {
        System.out.println(SingletonForHungry.singletonInstance);
        System.out.println(SingletonForHungry.singletonInstance);
    }
}

2、懒汉式(锁式)
public class SingletonForLazySynchronized {
    // 声明此类型的变量,但没有实例化
    private static SingletonForLazySynchronized singletonInstance;

    // 私有化所有的构造方法,防止直接通过new关键字实例化
    private SingletonForLazySynchronized() {

    }

    // 对外提供一个获取实例的静态方法,为了数据安全添加synchronized关键字
    public static SingletonForLazySynchronized getSingletonInstance() {
        if (singletonInstance == null) {
            singletonInstance = new SingletonForLazySynchronized();
        }
        return singletonInstance;
    }

    public static void main(String[] args) {

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                System.out.println(SingletonForLazySynchronized.getSingletonInstance());
            }).start();
        }
    }
}

3、懒汉式(双重检测锁式)
public class SingletonForLazyDoubleCheck {

    // 声明此类型的变量,但没有实例化
    // 必须加volatile,防止指令重排序
    private static volatile SingletonForLazyDoubleCheck singletonInstance;

    // 私有化所有的构造方法,防止直接通过new关键字实例化
    private SingletonForLazyDoubleCheck() {
    }

    // 对外提供一个获取实例的静态方法
    public static SingletonForLazyDoubleCheck getSingletonInstance() {
        if (singletonInstance == null) {
            synchronized (SingletonForLazyDoubleCheck.class) {
                // 双重检测
                if (singletonInstance == null) {
                    // 加volatile防止指令重排序
                    // 1.分配内存空间
                    // 2.创建实例对象
                    // 3.把这个内存地址赋值给变量的引用
                    singletonInstance = new SingletonForLazyDoubleCheck();
                }
            }
        }
        return singletonInstance;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(DoubleCheckSingleton.getSingletonInstance());
        System.out.println(DoubleCheckSingleton.getSingletonInstance());
    }

}
4、静态内部类式
public class SingletonForStaticInner {

    // 静态内部类
    public static class SingletonClassInstance {
        // 声明外部类型的静态常量
        public static final SingletonForStaticInner singletonInstance = new SingletonForStaticInner();
    }

    // 私有化构造方法
    private SingletonForStaticInner() {

    }

    // 对外提供的唯一获取实例的方法
    public static SingletonForStaticInner getSinletonInstance() {
        return SingletonClassInstance.singletonInstance;
    }

    public static void main(String[] args) {
        System.out.println(SingletonForStaticInner.getSinletonInstance());
        System.out.println(SingletonForStaticInner.getSinletonInstance());
    }
}

5、枚举单例
public enum SingletonForEnum {
    INSTANCE;

    public static void main(String[] args) {
        System.out.println(SingletonForEnum.INSTANCE);
        System.out.println(SingletonForEnum.INSTANCE);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/890977.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号