public void TestPerformance_Of_DoubleLock_And_SingleLock()
        {
            DoubleCheckLockingSingleton doubleCheckSingleton = null;
            var stopWatch = System.Diagnostics.Stopwatch.StartNew();

            stopWatch.Start();
            for (int i = 0; i < invokeCounter; i++)
            {
                doubleCheckSingleton = DoubleCheckLockingSingleton.GetSingletonInstance();
            }
            stopWatch.Stop();
            var elapsedDoubleCheck = stopWatch.ElapsedMilliseconds;

            SingleCheckSingleton singleCheckSingleton = null;

            stopWatch.Start();
            for (int i = 0; i < invokeCounter; i++)
            {
                singleCheckSingleton = SingleCheckSingleton.GetSingletonInstance();
            }
            stopWatch.Stop();
            var elapsedSingleCheck = stopWatch.ElapsedMilliseconds;

            Assert.IsTrue(elapsedDoubleCheck < elapsedSingleCheck);
        }
        public void CheckIf_DoubleCheckSingletonInstances_AreEqual()
        {
            DoubleCheckLockingSingleton singleton       = DoubleCheckLockingSingleton.GetSingletonInstance();
            DoubleCheckLockingSingleton secondSingleton = DoubleCheckLockingSingleton.GetSingletonInstance();

            Assert.AreEqual(singleton, secondSingleton);
        }
 public void CheckIf_DoubleCheckSingleton_Contains_OneInstance_Parallel()
 {
     Parallel.For(0, numberOfThreads, task =>
     {
         DoubleCheckLockingSingleton.GetSingletonInstance();
     });
     Assert.AreEqual(1, DoubleCheckLockingSingleton.SingletonInstances);
 }
        public void CheckIf_DoubleCheckSingleton_Contains_OneInstance()
        {
            for (int i = 0; i < invokeCounter; i++)
            {
                DoubleCheckLockingSingleton.GetSingletonInstance();
            }

            Assert.AreEqual(1, DoubleCheckLockingSingleton.SingletonInstances);
        }
Ejemplo n.º 5
0
        public static DoubleCheckLockingSingleton GetSingletonInstance()
        {
            if (singletonInstance == null)
            {
                lock (locker)
                {
                    if (singletonInstance == null)
                    {
                        singletonInstance   = new DoubleCheckLockingSingleton();
                        SingletonInstances += 1;
                    }
                }
            }

            return(singletonInstance);
        }
        public void TestThredsPerformance_Of_DoubleLock_And_SingleLock()
        {
            var stopWatch = System.Diagnostics.Stopwatch.StartNew();

            stopWatch.Start();
            Parallel.For(0, numberOfThreads, task =>
            {
                DoubleCheckLockingSingleton.GetSingletonInstance();
            });
            stopWatch.Stop();
            var elapsedDoubleCheck = stopWatch.ElapsedTicks;

            stopWatch.Start();
            Parallel.For(0, numberOfThreads, task =>
            {
                SingleCheckSingleton.GetSingletonInstance();
            });
            stopWatch.Stop();
            var elapsedSingleCheck = stopWatch.ElapsedTicks;

            Assert.IsTrue(elapsedDoubleCheck < elapsedSingleCheck, string.Format("Elapsed single: {0}, double: {1}",
                                                                                 elapsedSingleCheck, elapsedDoubleCheck));
        }