zoukankan      html  css  js  c++  java
  • singleton pattern的推荐实现

    一、单例模式的C#实现:

    (1)使用double-checked locking的方式:

    public sealed class Singleton
    {
        private static volatile Singleton instance = null;
        private static readonly object padlock = new object();
    
        private Singleton()
        {
        }
    
        public static Singleton Instance
        {
            get
            {
                if (instance==null)
                {
                    lock (padlock)
                    {
                        if (instance==null)
                        {
                            instance = new Singleton();
                        }
                    }
                }
                return instance;
            }
        }
    }

    这种实现方式对多线程来说是安全的,同时线程不是每次都加锁,只有判断对象实例没有被创建时它才加锁。注意volatile的使用,只有加了volatile修饰,才能保证在多线程环境下只有一个实例。

    因为使用了锁,没有下面的方式高效。

    public sealed class Singleton
    {
        private static Singleton instance = null;
    
        private Singleton()
        {
        }
    
        public static Singleton GetInstance()
        {
            if(instance!=null) return instance;
            
            Singleton temp = new Singleton();
            Interlocked.CompareExchange(ref instance,temp,null);
            
            return instance;
        }
    }

    如果多个线程同时调用GetInstance(),会产生多个Singleton对象,但是Interlocked.CompareExchange()会保证只有一个引用赋值给instance,没有引用到的对象会被垃圾回收。

    (2)使用静态成员初始化方式(thread-safe without using locks)

    public sealed class Singleton
    {
        private static readonly Singleton instance = new Singleton();
    
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Singleton()
        {
        }
    
        private Singleton()
        {
        }
    
        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }
    }

    由于Instance是static的,保证在AppDomain只有一个实例。

    初始化的线程安全性是由.Net保证的。

    手动加上一个static的constructor,以让.Net对它进行延迟加载,但并不是完全延迟初始化,当该单例有其他static成员被使用时,该instance就被创建,而不是该instance被使用时才被创建。

    (3)推荐方式:使用内部一个Instance的Holder来“持有”单例

    public sealed class Singleton
    {
        private Singleton()
        {
        }
    
        public static Singleton Instance
        {
            get
            {
                return Nested.instance;
            }
        }
        
        class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
            }
    
            internal static readonly Singleton instance = new Singleton();
        }
    }

    这种实现方式不需要加锁(without using locks)。

    这种方式是完全延迟初始化(fully lazy instantiation),该Instance只有被使用时才被创建。

    (4)最简单有效方式:

    /// <summary>
    /// .NET 4's Lazy<T> type
    /// </summary>
    public sealed class Singleton
    {
        private static readonly Lazy<Singleton> lazy =
            new Lazy<Singleton>(() => new Singleton());
    
        public static Singleton Instance { get { return lazy.Value; } }
    
        private Singleton()
        {
        }
    }

    这种方式的简单和性能良好,而且还提供检查是否已经创建实例的属性IsValueCreated。

    二、单例模式的Java实现:

    (1)使用double-checked locking的方式:

    public class Singleton {
    
        private static volatile Singleton instance = null;
    
        private Singleton() { }
    
        public static Singleton getInstance() {
            if (instance == null) {
                synchronized (Singleton.class) {
                    if (instance == null) {
                        instance = new Singleton();
                    }
                }
            }
            return instance;
        }
    }

    双重检验锁模式(double checked locking pattern), 是一种使用同步块加锁的方法。程序员称其为双重检查锁,因为会有两次检查 instance == null ,一次是在同步块外,一次是在同步块内。 为什么在同步块内还要再检验一次?因为可能会有多个线程一起进入同步块外的 if, 如果在同步块内不进行二次检验的话就会生成多个实例了。

    注意:这里的volatile修饰是必不可少的,当一个变量被volatile修饰后, 它将具备两种特性:

    1. 线程可见性:当一个线程修改了被volatile修饰的变量后,无论是否加锁,其它线程都可以立即看到最新的修改,而普通变量却做不到这点;

    2. 禁止指令重排序优化:普通的变量仅仅保证在该方法的执行过程中所有依赖赋值结果的地方都能获取正确的结果,而不能保证变量赋值操作的顺序与程序代码的执行顺序一致。 

    (2)使用Eager initialization的方式:

    public class Singleton 
    {
        private static final Singleton instance = new Singleton();
     
        private Singleton() {}
     
        public static Singleton getInstance() {
            return instance; 
        }
    }

    (3)推荐实现:Initialization On Demand Holder Idiom

    public class Singleton 
    {
        // Private constructor prevents instantiation from other classes
        private Singleton() { }
     
        /**
        * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
        * or the first access to SingletonHolder.INSTANCE, not before.
        */
        private static class SingletonHolder { 
            private static final Singleton instance = new Singleton();
        }
     
        public static Singleton getInstance() {
            return SingletonHolder.instance;
        }
    }

    这种写法仍然使用JVM本身机制保证了线程安全问题,由于 SingletonHolder 是私有的,除了getInstance()之外没有办法访问它,因此它是Lazy Initialized的,同时读取实例的时候不会进行同步,没有性能缺陷,也不依赖 JDK 版本。

    (4)最简单实现:使用一个enum来实现,线程安全性由JVM保证。

    public enum SingletonEnum {
        Instance;
        
        private SingletonEnum()
        {
            // Some initialize code
        }
    }

    可以通过SingletonEnum.Instance来访问实例,这比调用getInstance()方法简单多了。 创建枚举默认就是线程安全的,所以不需要担心double checked locking, 而且还能防止反序列化导致重新创建新的对象。

    参考:

    http://en.wikipedia.org/wiki/Singleton_pattern

    http://www.yoda.arachsys.com/csharp/singleton.html

    http://www.cnblogs.com/rush/archive/2011/10/30/2229565.html

    http://javarevisited.blogspot.gr/2012/07/why-enum-singleton-are-better-in-java.html

    http://javarevisited.blogspot.sg/2011/03/10-interview-questions-on-singleton.html

  • 相关阅读:
    python数据类型
    集合(set)内置方法
    python第三天
    剑指offer-什么是1G/2G/3G/4G/5G
    经典交换实验-二层交换机实现pc隔离&vlan通信
    linux运维神器-htop&mtr
    三分钟速学linux-进程管理命令
    三分钟速学文件权限管理
    三分钟速学网卡管理配置-nmcli命令
    三分钟速学linux-centos/redhat常见包管理器
  • 原文地址:https://www.cnblogs.com/luxiaoxun/p/3841223.html
Copyright © 2011-2022 走看看