美文网首页
单例模式Java篇

单例模式Java篇

作者: maoai_xianyu | 来源:发表于2020-08-10 11:32 被阅读0次
    • 单例设计模式- 饿汉式
     public class Singleton1 {
          // 随着类的加载就已经new了对象
          private static Singleton1 mInstance = new Singleton1();
          private Singleton1() {
          }
          public static Singleton1 getInstance() {
               return mInstance;
          }
    }
    
    • 单例设计模式 - 懒汉式
    /**
       * @Description 单例设计模式 - 懒汉式
       */
    public class Singleton2 {
        // 使用的时候才会去new对象,可能更加高效
        // 会有问题?多线程并发的问题,如果多个线程调用会有多个实例
        private static Singleton2 mInstance;
        private Singleton2() {
        }
        public static Singleton2 getInstance() {
             if (mInstance == null) {
                   mInstance = new Singleton2();
             }
              return mInstance;
          }
    }
    
    • 单例设计模式 - 懒汉式 - 多线程并发
    /**
       * @Description 单例设计模式 - 懒汉式 - 多线程并发
      */
    public class Singleton3 {
        // 使用的时候才会去new对象,可能更加高效
       // 会有问题?多线程并发的问题,如果多个线程调用会有多个实例 如何解决
       private static Singleton3 mInstance;
       private Singleton3() {
       }
       // 同步锁,解决了线程安全的问题。但是会出现效率的问题?
       // 效率比较低,每次获取都要经过同步锁的判断 看 Singleton4
       public static synchronized Singleton3 getInstance() {
            if (mInstance == null) {
                  mInstance = new Singleton3();
            }
            return mInstance;
         }
    }
    
    • 单例设计模式 - 懒汉式 - 多线程并发 双重检测 (同步锁DCL) (常用)
    /**
       * @Description 单例设计模式 - 懒汉式 - 多线程并发 双重检测 (同步锁DCL)
     */
     public class Singleton4 {
        // 使用的时候才会去new对象,可能更加高效
       // 会有问题?多线程并发的问题,如果多个线程调用会有多个实例 如何解决
       // 添加 volatile 的用处是什么?
       // 1. 防止重排序
       // 2. 线程可见性-某一个线程改了公用对象(变量),短时间内另一个线程可能是不可见的,因为每一个线程都有自己的缓存区(线程工作区)
        private static volatile Singleton4 mInstance;
        private Singleton4() {
        }
        // Singleton4 mInstance = new Singleton4(); 
        // 1. 开辟一块空间(内存)
        // 2. 初始化对象
        // 3. 给变量赋值(指向内存地址)
        // 但是 2 和 3 在java多线程顺序是不固定的  volatile 为了防止 2和3互换位置
        // 1. 开辟一块空间(内存)
        // 2. 给变量赋值(指向内存地址)
        // 3. 初始化对象
        // 同步锁,解决了线程安全的问题。但是会出现效率的问题?
        // 既保证线程安全同时效率比较高
        // 这种方式还是会有问题的? 用 volatile 解决
        public static Singleton4 getInstance() {
            if (mInstance == null) {
                synchronized (Singleton4.class) {
                    if (mInstance == null) {
                        mInstance = new Singleton4();
                    }
                }
            }
            return mInstance;
        }
    }
    
    • 单例设计模式 - 静态内部类(常用)
    /**
      * @Description 单例设计模式 - 静态内部类(比较常用)
    */
    public class Singleton5 {
        private Singleton5() {
        }
        private static class Singleton5Inner {
            public static volatile Singleton5 mInstance = new Singleton5();
        }
        public static Singleton5 getInstance() {
            return Singleton5Inner.mInstance;
        }
    }
    
    • 单例设计模式 - 枚举
    /**
     * @Description  枚举单例模式
     */
    enum class SingleTonEnum {
        INSTANCE;
        fun doSomething() {
            println("do some thing")
        }
    }
    
    • 单例设计模式 - 容器管理 - Android源码 SystemServiceRegistry 获取系统的服务
    /**
      * @Description 单例设计模式 - 容器管理 - SystemServiceRegistry 获取系统的服务
      */
    public class Singleton7 {
        private static Map<String, Object> mSingleMap = new HashMap<>();
        static {
            mSingleMap.put("activity_manager", new Singleton7()); 
        }
        private Singleton7() {}
        public static Object getService(String serviceName) {
            return mSingleMap.get(serviceName);
        }
    }
    
    • 单例设计模式 - 自己写
    /**
      * @Description 单例设计模式
    */
    public class Singleton8 {
        private static Singleton8 mInstance;
        static {
            mInstance = new Singleton8();
        }
        private Singleton8() {}
        public static Singleton8 getInstance() {
            return mInstance;
        }
    }
    
    • volatile关键字
    1. 防止重排序
    2. 线程可见性-某一个线程改了公用对象(变量),短时间内另一个线程可能是不可见的,因为每一个线程都有自己的缓存区(线程工作区)
    3. 测试volatile
    public class VolatileTest {
        public static void main(String[] args) {
            ThreadRun threadRun = new ThreadRun();
            new Thread(threadRun).start();
            while (true) {
                if (threadRun.isFlag()) {
                    System.out.println("--------跳出循环------");
                    break;
                }
            }
            // 没有个 flag 设置 Volatile 结果一直是  flag  true 不会执行 --------跳出循环------
        }
        static class ThreadRun implements Runnable {
            private volatile boolean flag = false;
            @Override
            public void run() {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
                flag = true;
                System.out.println(" flag " + isFlag());
            }
            public boolean isFlag() {
                return flag;
            }
            public void setFlag(boolean flag) {
                this.flag = flag;
               }
          }
    }
    

    注意

    1. 自己学习,不为盈利
    2. 如有侵权,可以联系删除

    相关文章

      网友评论

          本文标题:单例模式Java篇

          本文链接:https://www.haomeiwen.com/subject/nnnddktx.html