zoukankan      html  css  js  c++  java
  • 单例的使用

    没有并发的情况下使用单例:

    public sealed class Singleton
        {
            private static Singleton l_singleton;
            public static Singleton GetSingleton()
            {
                if(l_singleton==null)
                {
                    l_singleton = new Singleton();
                }
                return l_singleton;
            }
        }

    有并发的情况下,可以使用下面的单例:

        public sealed class Singleton
        {
            private static Singleton l_singleton;
            private readonly static object l_obj = new object();
            public static Singleton GetSingleton()
            {
                if(l_singleton==null)
                {
                    lock(l_obj)
                    {
                        if(l_singleton==null)
                        {
                            l_singleton = new Singleton();
                        }
                    }
                }
                return l_singleton;
            }
        }
  • 相关阅读:
    CSS样式表
    lianxi!
    传值
    lei!
    3.10
    if else&& stwitch break
    if else 语句
    2016.3.6
    进制转换
    PHP 面向对象的三大特征
  • 原文地址:https://www.cnblogs.com/namejr/p/11537764.html
Copyright © 2011-2022 走看看