• java多线程编程核心技术——第六章总结


    目录


     

    1.0立即加载/“饿汉式”

      立即加载:实用类的时候已经将对象创建完毕,常见的实现方法就是直接new实例化。

      注:是在调用方法前,就已经实例化了(通常是在类一加载的时候就已经实例化了)。

    public class Singleton {
        //立即加载方法==饿汉式
        private static Singleton singleton = new Singleton();
    
        private Singleton() {
        }
    
        public static Singleton getInstance() {
            //此代码版本为立即加载
            //此代码版本缺点:是不能有其他实例变量
            //因为getInstance()方法没有同步
            //所以可能出现非线程安全问题
            return singleton;
        }
    }

    2.0延迟加载/“懒汉式”

      延迟加载:在调用get()方法时实例才被创建,常见的实现办法就是在get()中进行实例化。

      注:在调用获取实例的方法时,实例才被创建。

    public class Singleton {
        private static Singleton singleton;
    
        private Singleton(){}
    
        public static Singleton getInstance() {
            //延迟加载,若多个线程在此if失去执行权,最终会产生很多实例对象
            if (singleton == null) {
                singleton = new Singleton();
            }
            return singleton;
        }
    }

      可以使用synchronized方法或者synchronized同步代码块解决多线程中的同步问题,但是效率较低。

      书中提供了双检查锁机制:

      可以使用DCL双检查锁机制实现单例:

    public class Singleton {
        private volatile static Singleton singleton;
    
        private Singleton(){}
        //使用双检查锁机制来解决问题,既保证了不需要使用同步代码块的异步执行性,又保证了单例效果。
    
        public static Singleton getInstance() {
            if (singleton == null) {
                synchronized (Singleton.class) {
                    if (singleton == null) {
                        singleton = new Singleton();
                    }
                }
            }
            return singleton;
        }
    }

      注:关于双检索的争议还是有很多的,但是目前可以认为其实安全的。

      双检查锁的安全性问题总结(未完成)


     

    3.0使用静态内置类实现单例模式

    public class Singleton {
        //内部类形式
        private static class MyHanlder {
            private static Singleton singleton = new Singleton();
        }
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            return MyHanlder.singleton;
        }
    }

    4.0序列化与反序列化的单例模式实现

      虽然静态内置类可以达到线程安全,但如果遇到序列化对象,使用默认的方式运行得到的还是多例。

    public class Singleton implements Serializable{
        private static final long seroalVersionUID = 888L;
    
        //内部类形式
        private static class MyHanlder {
            private static Singleton singleton = new Singleton();
        }
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            return MyHanlder.singleton;
        }
    
        protected Object readResolve() throws ObjectStreamException {
            return MyHanlder.singleton;
        }
    }

      书写readResolve()方法就可以避免在序列化与反序列化的过程中单例失效。

      注:目前为什么这样还不理解。


     

    5.0使用static代码块实现单例模式

      静态代码块中的代码在使用类的时候就已经执行了,所以可以应用静态代码块的这个特性来实现单例设计模式。

    public class Singleton implements Serializable{
        private static Singleton singleton = null;
    
        private Singleton() {}
    
        static {
            singleton = new Singleton();
        }
    
        public static Singleton getInstance() {
            return singleton;
        }
    }

     


     

    6.0使用enum枚举数据类型实现单例模式

      枚举与静态代码块类似,在使用枚举时,构造方法会被自动调用,也可以使用其这个特性实现单例设计模式。


    本文内容是书中内容兼具自己的个人看法所成。可能在个人看法上会有诸多问题(毕竟知识量有限,导致认知也有限),如果读者觉得有问题请大胆提出,我们可以相互交流、相互学习,欢迎你们的到来,心成意足,等待您的评价。

     

  • 相关阅读:
    C++ string用法
    C++ 静态变量及函数的生命周期
    C++ const的用法和作用
    C++ 指针和引用的区别
    C++ struct 和 Class的区别
    C++对象模型-构造函数语意学
    大端模式与小端模式、网络字节顺序与主机字节顺序
    Spring Boot系列——Spring Boot如何启动
    分库分表利器——sharding-sphere
    并发和多线程-八面玲珑的synchronized
  • 原文地址:https://www.cnblogs.com/lilinzhiyu/p/8073906.html
Copyright © 2020-2023  润新知