Esempio n. 1
0
        public static void Main(string[] args)
        {
            var singleton = Singleton.GetInstance();

            singleton = Singleton.GetInstance();

            var singletonSafe = SingletonSafe.GetInstance();

            singletonSafe = SingletonSafe.GetInstance();

            var singletonSafe2 = SingletonSafe2.GetInstance();

            singletonSafe2 = SingletonSafe2.GetInstance();

            var singletonReadOnly = SingletonReadOnly.GetInstance();

            singletonReadOnly = SingletonReadOnly.GetInstance();

            var singletonLazy = SingletonLazy.GetInstance();

            singletonLazy = SingletonLazy.GetInstance();

            var singletonGeneric = SingletonGeneric.GetInstance();

            singletonGeneric = SingletonGeneric.GetInstance();

            var singletonGeneric2 = SingletonGeneric2.GetInstance();

            singletonGeneric2 = SingletonGeneric2.GetInstance();

            Console.ReadKey();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var stopwatch = new Stopwatch();

            // ====== SingletonGetterLock =======

            stopwatch.Start();

            SingletonGetterLock singleton1 = null;

            Parallel.For(0, 10, task =>
            {
                for (int i = 0; i < 1_000_000; i++)
                {
                    singleton1 = SingletonGetterLock.Instance;
                }
            });

            stopwatch.Stop();
            Console.WriteLine($"Number of ticks for SingletonGetterLock: {stopwatch.ElapsedTicks}");

            stopwatch.Reset();

            // ====== SingletonDoubleCheck ======

            stopwatch.Start();

            SingletonDoubleCheck singleton2 = null;

            Parallel.For(0, 10, task =>
            {
                for (int i = 0; i < 1_000_000; i++)
                {
                    singleton2 = SingletonDoubleCheck.Instance;
                }
            });

            stopwatch.Stop();
            Console.WriteLine($"Number of ticks for SingletonDoubleCheck: {stopwatch.ElapsedTicks}");

            stopwatch.Reset();

            // ====== SingletonLazy =============

            stopwatch.Start();

            SingletonLazy singleton3 = null;

            Parallel.For(0, 10, task =>
            {
                for (int i = 0; i < 1_000_000; i++)
                {
                    singleton3 = SingletonLazy.Instance;
                }
            });

            stopwatch.Stop();
            Console.WriteLine($"Number of ticks for SingletonLazy: {stopwatch.ElapsedTicks}");

            Console.ReadKey();
        }