多线程下的单子模式

class MSingleTon
    {
        private static MSingleTon instance;
        private static readonly object syncRoot = new object();
        private MSingleTon()
        {
        }
        public static MSingleTon GetMSingleTon()
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if(instance==null)
                    instance = new MSingleTon();
                }
            }
            return instance;
        } 
    }
    public sealed class SingleTon1
    {
        private static readonly SingleTon1 instance=new SingleTon1();
        private SingleTon1()
        {
        }
        public static SingleTon1 GetSingleTon1()
        {
            return instance;
        }
      
    }

posted on 2009-04-24 10:37  Paul_  阅读(226)  评论(0编辑  收藏  举报

导航