设计模式-单例模式(四)

1.单例(满足多线程)

 public class
   {
        private static SingelDesign _intance = null;
        private static readonly object _lock = new object();

        public static SingelDesign Intance
        {
            get
            {
                return _intance;
            }

            set
            {
                _intance = value;
            }
        }

        public static SingelDesign Get()
        {
            if (Intance == null)//这个判断为了多线程节省资源
            {
                lock (_lock)//加锁为了线程安全
                {
                    if (Intance == null)
                    {
                        Intance = new SingelDesign();
                    }
                }
            }
            return Intance;
        }
        private SingelDesign() { }
    }

2.调用

SingelDesign.Get();
萌橙 你瞅啥?
原文地址:https://www.cnblogs.com/daimaxuejia/p/12082523.html