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

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();

 

posted @ 2019-12-23 10:38  萌橙  阅读(110)  评论(0编辑  收藏  举报