private static void TestGenericSingleton()
        {
            Console.WriteLine("泛型单例模式:");

            Singleton4 instance = GenericSingleton <Singleton4> .GetInstance();

            instance.GetInfo();

            var singleton4 = Singleton4.Instance;

            singleton4.GetInfo();
            Console.ReadLine();
        }
예제 #2
0
 public static Singleton4 GetSingleton4()
 {
     if (_singleton4 == null)
     {
         lock (LockHelper)
         {
             if (_singleton4 == null)
             {
                 _singleton4 = new Singleton4();
             }
         }
     }
     return(_singleton4);
 }
예제 #3
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);
        }
예제 #4
0
        static void Main(string[] args)
        {
            Singleton4 instanc1 = null;
            Singleton4 instanc2 = null;
            Singleton4 instanc3 = null;
            Singleton4 instanc4 = null;

            new Thread(() =>
            {
                instanc1 = Singleton4.GetInstance();
            }).Start();

            new Thread(() =>
            {
                instanc2 = Singleton4.GetInstance();
            }).Start();

            new Thread(() =>
            {
                instanc3 = Singleton4.GetInstance();
            }).Start();

            new Thread(() =>
            {
                instanc4 = Singleton4.GetInstance();
            }).Start();


            Thread.CurrentThread.Join(500);
            if ((instanc1 == instanc2) && (instanc3 == instanc4))
            {
                Console.WriteLine("singleton");
            }
            else
            {
                Console.WriteLine("singleton error");
            }

            Console.Read();
        }