zoukankan      html  css  js  c++  java
  • 5、单例模式

      单例模式定义:确保一个类只有一个实例,并提供线程安全的访问点。
      
      以下介绍6中线程安全的单例模式
    • 立即加载模式/饿汉模式
    • 通过反射的方式可以获得多个实例
    public class Singleton1 {
    
        private static Singleton1 singleton1 = new Singleton1();
    
        private Singleton1() {
    
        }
    
        public static Singleton1 getSingleton() {
            return singleton1;
        }
    }
    • 使用静态代码块实现单例模式
    public class Singleton5 {
    
        private static Singleton5 singleton5;
    
        private Singleton5() {
    
        }
        static {
            singleton5 = new Singleton5();
        }
    
        public static Singleton5 getSingleton() {
            return singleton5;
        }
    }
    • 延迟加载模式/懒汉模式
    public class Singleton2 {
    
        private static Singleton2 singleton2;
    
        private Singleton2() {
    
        }
    
        public static synchronized Singleton2 getSingleton() {
            if(singleton2==null) {
                singleton2 = new Singleton2();
            }
            return singleton2;
        }
    }
    • 使用静态内部类方式
    • 如果遇到序列化对象时,还是可以获得多个实例对象
    public class Singleton3 {
    
        private static class Singleton{
            public static Singleton3 singleton= new Singleton3();
        }
    
        private Singleton3() {
    
        }
    
        public static Singleton3 getSingleton() {
            return Singleton.singleton;
        }
    }
    解决静态内部类通过序列化和反序列导致的多实例问题,如下:
    public class Singleton3 implements Serializable {
        private static final long serUID = 888L;
    
        private static class Singleton{
            public static Singleton3 singleton= new Singleton3();
        }
    
        private Singleton3() {
    
        }
    
        public static Singleton3 getSingleton() {
            return Singleton.singleton;
        }
    
        protected Object readResolve() throws ObjectStreamException {
            return Singleton.singleton;
        }
    }
    • 使用枚举类方式获得单例
    • 枚举类和静态代码块特性相似,在使用枚举类时,构造方法会被自动使用。
    public enum Singleton6 {
    
        singleton6;
    
        private Singleton6(){
    
        }
    
        public Singleton6 getSingleton(){
            return singleton6;
        }
    
    }
    • 使用DCL双检查锁机制,解决“延迟加载模式/懒汉模式”中使用synchronized锁导致的性能问题;
    public class Singleton4 {
    
        private volatile static Singleton4 singleton4;
    
        private Singleton4() {
    
        }
    
        public static Singleton4 getSingleton() {
            if(singleton4==null) {
                synchronized (Singleton4.class) {
                    singleton4 = new Singleton4();
                }
            }
            return singleton4;
        }
    }
     
    收藏文章数量从多到少与“把书读薄”是一个道理
  • 相关阅读:
    [状压dp][spfa] Jzoj P3737 挖宝藏
    [计算几何] Jzoj P3736 数学题
    [排序][vector] Jzoj P6288 旋转子段
    [区间dp] Jzoj P6287 扭动的树
    [bfs][spfa] Jzoj P6286 走格子
    [点分治] Luogu P2664 树上游戏
    [树链剖分][树状数组] Luogu P3676 小清新数据结构题
    [计算几何][dp] Luogu P1995 智能车比赛
    [后缀数组][并查集] Luogu P2178 品酒大会
    [莫比乌斯反演][整除分块] Bzoj P2301 Problem b
  • 原文地址:https://www.cnblogs.com/use-D/p/9757746.html
Copyright © 2011-2022 走看看