示例#1
0
        static void Main(string[] args)
        {
            //
            var company1 = Singleton1.GetInstance("Company 1");
            var company2 = Singleton1.GetInstance("Company 2");

            Console.WriteLine(company1.Name); // 1
            Console.WriteLine(company2.Name); // 1

            //
            (new Thread(() =>
            {
                var os1 = Singleton2.GetInstance("Windows 10");
                Console.WriteLine(os1.Name);
            })).Start();
            var os2 = Singleton2.GetInstance("Ubuntu");

            Console.WriteLine(os2.Name);

            //
            (new Thread(() =>
            {
                var createDate1 = Singleton3.GetInstance();
                Console.WriteLine(createDate1.Date.Ticks);
            })).Start();
            var createDate2 = Singleton3.GetInstance();

            Console.WriteLine(createDate2.Date.Ticks);

            //
            (new Thread(() =>
            {
                var modifyDate1 = Singleton4.GetInstance();
                Console.WriteLine(modifyDate1.Date.Ticks);
            })).Start();
            var modifyDate2 = Singleton4.GetInstance();

            Console.WriteLine(modifyDate2.Date.Ticks);

            //
            (new Thread(() =>
            {
                var lazy1 = Singleton5.GetInstance();
                Console.WriteLine(lazy1.Id);
            })).Start();
            var lazy2 = Singleton5.GetInstance();

            Console.WriteLine(lazy2.Id);
        }
示例#2
0
        public static void Main(string[] args)
        {
            //0
            var i0 = Singleton.GetInstance();

            Console.WriteLine($"Singleton {ReferenceEquals(i0, Singleton.GetInstance())}");
            //1
            var i1 = Singleton1.GetInstance();

            Console.WriteLine($"Singleton1 {ReferenceEquals(i1, Singleton1.GetInstance())}");
            //2
            var i2 = Singleton2.GetInstance();

            Console.WriteLine($"Singleton2 {ReferenceEquals(i2, Singleton2.GetInstance())}");

            Console.ReadLine();
        }
示例#3
0
        public static void Main(string[] args)
        {
            // //0
            // var i0 = Singleton.GetInstance();
            // Console.WriteLine($"Singleton {ReferenceEquals(i0, Singleton.GetInstance())}");
            // //1
            // var i1 = Singleton1.GetInstance();
            // Console.WriteLine($"Singleton1 {ReferenceEquals(i1, Singleton1.GetInstance())}");
            // //2
            // var i2 = Singleton2.GetInstance();
            // Console.WriteLine($"Singleton2 {ReferenceEquals(i2, Singleton2.GetInstance())}");

            Console.WriteLine($"Singleton");
            Enumerable.Range(1, 10).Select(i => Task.Run(() =>
            {
                Console.WriteLine($"{Singleton.GetInstance().GetHashCode()}");
            })).WhenAll().Wait();

            Console.WriteLine($"Singleton1");
            Enumerable.Range(1, 10).Select(i => Task.Run(() =>
            {
                Console.WriteLine($"{Singleton1.GetInstance().GetHashCode()}");
            })).WhenAll().Wait();

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

            Console.WriteLine($"Singleton3");
            Enumerable.Range(1, 10).Select(i => Task.Run(() =>
            {
                Console.WriteLine($"{Singleton3.GetInstance().GetHashCode()}");
            })).WhenAll().Wait();

            Console.ReadLine();
        }