示例#1
0
        static void Main(string[] args)
        {
            NaiveSingleton s1 = NaiveSingleton.GetInstance();
            NaiveSingleton s2 = NaiveSingleton.GetInstance();

            if (s1 == s2)
            {
                Console.WriteLine("NaiveSingleton works, both variables contain the same instance.");
            }
            else
            {
                Console.WriteLine("NaiveSingleton failed, variables contain different instances.");
            }
            s1.work();


            Thread process2 = new Thread(() =>
            {
                TestSingleton("BAR");
            });
            Thread process1 = new Thread(() =>
            {
                TestSingleton("FOO");
            });

            process1.Start();
            process2.Start();

            process1.Join();
            process2.Join();
        }
示例#2
0
 public static NaiveSingleton GetInstance()
 {
     if (_instance == null)
     {
         _instance = new NaiveSingleton();
     }
     return(_instance);
 }