zoukankan      html  css  js  c++  java
  • 23种设计模式之一(单例模式)

    23种设计模式之一(单例模式)


     

    概念

    一个类永远只有一个对象存在。

    实现方式

    方式1(饿汉式 - 线程安全)

    package mirale.luna.design.patterns.singleton;
    
    /**
     * Created by Miracle Luna on 2020/12/15
     *
     * 饿汉式:线程安全
     * 类加载到内存后,就实例化一个单例,JVM保证线程安全
     * 简单易用,推荐使用!
     * 唯一缺点:不管用到与否,类装载时就完成实例化
     */
    public class Singleton01 {
        private static Singleton01 instance = new Singleton01();
    
        private Singleton01(){}
    
        public static Singleton01 getInstance(){
            return instance;
        }
    
        /**
         * 通过查看 hashCode值 是否一致,判断是否线程安全
         * @param args
         */
        public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                new Thread(() -> System.out.println(Singleton01.getInstance().hashCode())).start();
            }
        }
    }

    方式2(懒汉式 - 线程不安全)

    package mirale.luna.design.patterns.singleton;
    
    /**
     * Created by Miracle Luna on 2020/12/15
     *
     * 懒汉式:线程不安全
     */
    public class Singleton02 {
        private static Singleton02 instance;
    
        private Singleton02() {}
    
        public static Singleton02 getInstance() {
            instance = new Singleton02();
            return instance;
        }
    
        /**
         * 通过查看 hashCode值 是否一致,判断是否线程安全
         * @param args
         */
        public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                new Thread(() -> System.out.println(Singleton02.getInstance().hashCode())).start();
            }
        }
    }

    方式3(懒汉式 - 线程安全 - 加锁 - 效率下降)

    package mirale.luna.design.patterns.singleton;
    
    /**
     * Created by Miracle Luna on 2020/12/15
     *
     * 懒汉式:线程安全(加锁实现,但是效率下降)
     */
    public class Singleton03 {
        private static Singleton03 instance;
    
        private Singleton03() {}
    
        public static synchronized Singleton03 getInstance() {
            if (instance == null) {
                instance = new Singleton03();
            }
            return instance;
        }
    
        /**
         * 通过查看 hashCode值 是否一致,判断是否线程安全
         * @param args
         */
        public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                new Thread(() -> System.out.println(Singleton03.getInstance().hashCode())).start();
            }
        }
    }

    方式4(懒汉式 - 线程安全 - DCL - 效率提升 - 较完美的实现方式

    package mirale.luna.design.patterns.singleton;
    
    /**
     * Created by Miracle Luna on 2020/12/15
     *
     * 懒汉式:线程安全
     * DCL: Double Check Loading
     * 较为完美的单例写法
     */
    public class Singleton04 {
        private static volatile Singleton04 instance; // volatile:防止指令重排
    
        private Singleton04() {}
    
        public static Singleton04 getInstance() {
            if (instance == null) {
                // 双重检查
                synchronized (Singleton04.class) {
                    if (instance == null) {
                        instance = new Singleton04();
                    }
                }
            }
            return instance;
        }
    
        /**
         * 通过查看 hashCode值 是否一致,判断是否线程安全
         * @param args
         */
        public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                new Thread(() -> System.out.println(Singleton04.getInstance().hashCode())).start();
            }
        }
    }

    方式5(内部类实现 - 线程安全 - 懒加载 - 较完美的实现方式

    package mirale.luna.design.patterns.singleton;
    
    /**
     * Created by Miracle Luna on 2020/12/15
     *
     * 静态内部类
     * JVM 保证单例,且线程安全,不用加锁
     * 加载外部类时不会加载内部类,变相实现了懒加载(Lazy Loading)
     * 较为完美的单例写法
     */
    public class Singleton05 {
    
        private Singleton05() {}
    
        private static class Singleton05Holder {
            private static final Singleton05 instance = new Singleton05();
        }
    
        public static Singleton05 getInstance() {
            return Singleton05Holder.instance;
        }
    
        /**
         * 通过查看 hashCode值 是否一致,判断是否线程安全
         * @param args
         */
        public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                new Thread(() -> System.out.println(Singleton05.getInstance().hashCode())).start();
            }
        }
    }

    方式6(枚举类实现 - 线程安全 - 防止反序列化 - 最完美的实现方式

    package mirale.luna.design.patterns.singleton;
    
    /**
     * Created by Miracle Luna on 2020/12/15
     *
     * 枚举单例
     * 不仅实现了线程同步,而且防止了反序列化
     * 最完美的单例写法
     */
    public enum Singleton06 {
        instance;
    
        /**
         * 通过查看 hashCode值 是否一致,判断是否线程安全
         * @param args
         */
        public static void main(String[] args) {
            for (int i = 0; i < 100; i++) {
                new Thread(() -> System.out.println(Singleton06.instance.hashCode())).start();
            }
        }
    }
  • 相关阅读:
    2015腾讯暑期实习笔试题目
    二叉树的优点和缺点
    pandas对象保存到mysql出错提示“BLOB/TEXT column used in key specification without a key length”解决办法
    事务的隔离机制
    Flink Sink定制开发
    Presto实现定时从配置文件读取配置
    LDAP与Sentry API使用
    Presto压测报告
    PrestoSPI安全扩展
    项目重构总结
  • 原文地址:https://www.cnblogs.com/miracle-luna/p/14136608.html
Copyright © 2011-2022 走看看