示例#1
0
        public static SingletonThreadSafe GetInstance()
        {
            // this will create two instances running in parallel
            //if (_instance == null)
            //{
            //    if (_instance == null)
            //    {
            //        _instance = new SingletonThreadSafe();
            //    }
            //}
            //return _instance;


            // double-checked locking for better performance
            if (_instance == null)
            {
                lock (_instanceLock) // lock lower the performance
                {
                    if (_instance == null)
                    {
                        _instance = new SingletonThreadSafe();
                    }
                }
            }
            return(_instance);
        }
示例#2
0
        public static void FromB()
        {
            var singleton2 = SingletonThreadSafe.GetInstance();

            singleton2.IncrementCount("B");
            singleton2.IncrementCount("B");
            singleton2.IncrementCount("B");
        }
示例#3
0
        public static void FromA()
        {
            var singleton1 = SingletonThreadSafe.GetInstance();

            singleton1.IncrementCount("A");
            singleton1.IncrementCount("A");
            singleton1.IncrementCount("A");
        }