Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine($"Singleton");
            Enumerable.Range(1, 5).Select(
                i => Task.Run(() =>
            {
                Console.WriteLine($"{Singleton.GetInstance().GetHashCode()}");
            })
                ).ToList();
            //Enumerable.Range(1, 100).AsParallel().ForAll(i => { Console.WriteLine(i); });
            Console.WriteLine($"Singleton1");
            Enumerable.Range(1, 5).Select(i => Task.Run(() =>
            {
                Console.WriteLine($"{Singleton1.GetInstance().GetHashCode()}");
            })).ToList();

            Console.WriteLine($"Singleton2");
            Enumerable.Range(1, 5).Select(i => Task.Run(() =>
            {
                Console.WriteLine($"{Singleton2.GetInstance().GetHashCode()}");
            })).ToList();

            Console.WriteLine($"Singleton3");
            var ss = Enumerable.Range(1, 5).Select(i => Task.Run(() =>
            {
                Console.WriteLine($"{Singleton3.GetInstance().GetHashCode()}");
            })).ToList();

            Console.ReadLine();
        }
Ejemplo n.º 2
0
        protected override void RealTestOutPut()
        {
            println(Singleton1.GetInstance().ToString());
            println(Singleton2.GetInstance().ToString());

            println(Singleton1.GetInstance().ToString());
            println(Singleton2.GetInstance().ToString());
        }
Ejemplo n.º 3
0
        public void TestSingleton2()
        {
            var instance1 = Singleton2.GetInstance();

            Assert.IsNotNull(instance1);
            var instance2 = Singleton2.GetInstance();

            Assert.AreSame(instance1, instance2);
        }
Ejemplo n.º 4
0
        private static void Demo(string[] args)
        {
            for (int i = 0; i < 1000; i++)
            {
                Task.Factory.StartNew(() =>
                {
                    SinglePerson p = SinglePerson.GetInstance();
                    Singleton1 p1  = Singleton1.GetInstance();
                    Singleton2 s2  = Singleton2.GetInstance();
                });
            }

            Console.ReadKey();
        }
Ejemplo n.º 5
0
        public static void TestInstance2Unique()
        {
            for (int i = 0; i < 5; i++)
            {
                int          threadCount = 10000;
                Task <int>[] tasks       = new Task <int> [threadCount];
                for (int j = 0; j < threadCount; j++)
                {
                    tasks[j] = Task.Run <int>(() =>
                    {
                        return(Singleton2.GetInstance().GetHashCode());
                    });
                }
                Task.WaitAll(tasks);

                var codes = tasks.Select(t => t.Result).Distinct();
                Debug.Assert(codes.Count() == 1, $"Singleton2非单例 {codes.Count()}");
            }
        }
Ejemplo n.º 6
0
        public static void TestInstanceProfile()
        {
            int  count = 100000;
            long es    = 0;
            var  watch = Stopwatch.StartNew();

            for (int i = 0; i < count; i++)
            {
                var va = Singleton1.GetInstance();
            }
            watch.Stop();
            es = watch.ElapsedTicks;
            Console.WriteLine($"1 耗时 {es}");

            watch.Restart();
            for (int i = 0; i < count; i++)
            {
                var va = Singleton2.GetInstance();
            }
            watch.Stop();
            es = watch.ElapsedTicks;
            Console.WriteLine($"2 耗时 {es}");

            watch.Restart();
            for (int i = 0; i < count; i++)
            {
                var va = Singleton3.GetInstance();
            }
            watch.Stop();
            es = watch.ElapsedTicks;
            Console.WriteLine($"3 耗时 {es}");

            watch.Restart();
            for (int i = 0; i < count; i++)
            {
                var va = Singleton4.GetInstance();
            }
            watch.Stop();
            es = watch.ElapsedTicks;
            Console.WriteLine($"4 耗时 {es}");
        }
        static void Main(string[] args)
        {
            var uno = Singleton2.GetInstance();

            Console.WriteLine(uno.ToString());
        }