Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Singleton s1 = Singleton.Instance;
            Singleton s2 = Singleton.Instance;

            Console.WriteLine("s1: " + s1.GetHashCode() + ", s2: " + s2.GetHashCode());
            s1.doSomething();
            s2.doSomething();


            Console.WriteLine("\n\nTesting Singleton Pattern use multiple threading.");
            Task.Run(() =>
            {
                System.Threading.Thread.Sleep(5000);
                SingletonThreadSafe s12 = SingletonThreadSafe.Instance;
                Console.WriteLine($"first: {s12.GetHashCode()}");
                s12.doSomething();
            });
            Task.Run(() =>
            {
                System.Threading.Thread.Sleep(5000);
                SingletonThreadSafe s22 = SingletonThreadSafe.Instance;
                Console.WriteLine($"second: {s22.GetHashCode()}");
                s22.doSomething();
            });
            Console.ReadLine();
        }