zoukankan      html  css  js  c++  java
  • 单例模式之双重锁模式、静态内部类模式、饿汉模式、懒汉模式,和安全的懒汉模式

    单例模式之双重锁模式

    /**
     * @program: IdeaJava
     * @Date: 2019/12/17 16:26
     * @Author: lhh
     * @Description: 双重锁
     */
    public class DoubleLockSingletonApp {
        public static void main(String[] args)
        {
    
        }
    }
    class DoubleLockSingleton{
        //使用volatile避免不必要的实例
        private static volatile DoubleLockSingleton doubleLockSingletonInstance;
        private DoubleLockSingleton(){}
        public static DoubleLockSingleton getDoubleLockSingletonInstance()
        {
            if(doubleLockSingletonInstance == null)
            {   //使用synchronized进行同步避免多线程问题
                synchronized(DoubleLockSingleton.class)
                {
                    if(doubleLockSingletonInstance == null)
                    {
                        return doubleLockSingletonInstance = new DoubleLockSingleton();
                    }
                }
            }
            return doubleLockSingletonInstance;
        }
    }

    单例模式之静态内部类模式

    /**
     * @program: IdeaJava
     * @Date: 2019/12/17 17:17
     * @Author: lhh
     * @Description:
     */
    public class StaticInnerSingletonApp {
        public static void main(String[] args) {
            StaticInnerSingleton instance1 = StaticInnerSingleton.getInstance();
            StaticInnerSingleton instance2 = StaticInnerSingleton.getInstance();
            System.out.println(instance1 == instance2);
        }
    }
    class StaticInnerSingleton
    {
        private StaticInnerSingleton(){}
        public static StaticInnerSingleton getInstance()
        {
            return Inner.instance;
        }
        private static class Inner
        {
            private static final StaticInnerSingleton instance = new StaticInnerSingleton();
        }
    }

    单例模式之饿汉模式

    /**
     * @program: IdeaJava
     * @Date: 2019/12/17 15:57
     * @Author: lhh
     * @Description: 饿汉单例模式
     */
    public class HungrySingletonApp {
        public static void main(String[] args)
        {
            HungrySingleton instance1 = HungrySingleton.getHungrySingletonInstance();
            HungrySingleton instance2 = HungrySingleton.getHungrySingletonInstance();
            System.out.println(instance1 == instance2);
        }
    }
    
    class HungrySingleton{
        private static HungrySingleton hungrySingletonInstance = new HungrySingleton();
        private HungrySingleton(){}
        public static HungrySingleton getHungrySingletonInstance()
        {
            return hungrySingletonInstance;
        }
    }

    单例模式之懒汉模式,和安全的懒汉模式

    /**
     * @program: IdeaJava
     * @Date: 2019/12/17 16:15
     * @Author: lhh
     * @Description: 懒汉单例模式,synchronized懒汉单例模式
     */
    public class LazySingletonApp {
        public static void main(String[] args)
        {
            LazySingleton instance1 = LazySingleton.getLazySingletonInstance();
            LazySingleton instance2 = LazySingleton.getLazySingletonInstance();
            System.out.println(instance1 == instance2);
    
            SafeLazySingleton instance3 = SafeLazySingleton.getSafeLazySingletonInstance();
            SafeLazySingleton instance4 = SafeLazySingleton.getSafeLazySingletonInstance();
            System.out.println(instance3 == instance4);
        }
    }
    
    class LazySingleton{
        private static LazySingleton lazySingletonInstance;
        private LazySingleton(){}
        public static LazySingleton getLazySingletonInstance()
        {
            if (lazySingletonInstance == null)
            {
                lazySingletonInstance = new LazySingleton();
            }
            return  lazySingletonInstance;
        }
    }
    
    //安全的懒汉模式
    class SafeLazySingleton{
        private  static SafeLazySingleton safeLazySingletonInstance;
        private SafeLazySingleton(){}
        //getSafeLazySingletonInstance在方法上加了synchronized
        public static synchronized SafeLazySingleton getSafeLazySingletonInstance()
        {
            if (safeLazySingletonInstance == null)
            {
                safeLazySingletonInstance = new SafeLazySingleton();
            }
            return safeLazySingletonInstance;
        }
    }
  • 相关阅读:
    DAY9 函数初识(各种参数的用法)
    CSS背景
    HTML/CSS 练习
    从JDBC到commons-DBUtils
    SQL
    MYSQL数据库基本操作
    JDBC
    Stream数据流(Collection接口扩充)
    Stack栈
    Map集合接口
  • 原文地址:https://www.cnblogs.com/lhh666/p/11568274.html
Copyright © 2011-2022 走看看