Esempio n. 1
0
        public static void firstExample()
        {
            ReservationSingleton instance = ReservationSingleton.GetInstance();

            Console.WriteLine("1st instace ID " + RuntimeHelpers.GetHashCode(instance));

            ReservationSingleton instance2 = ReservationSingleton.GetInstance();

            Console.WriteLine("2nd instace ID " + RuntimeHelpers.GetHashCode(instance2));
        }
Esempio n. 2
0
        public static ReservationSingleton GetInstance()
        {
            if (firstInstance == null)
            {
                lock (syncRoot){
                    if (firstInstance == null)
                    {
                        firstInstance = new ReservationSingleton();
                    }
                }
            }

            return(firstInstance);
        }
Esempio n. 3
0
        public static void secondExample()
        {
            Console.WriteLine("--------THREADS---------");

            new Thread(() =>
            {
                ReservationSingleton instance = ReservationSingleton.GetInstance();
                Console.WriteLine("1st thread instace ID " + RuntimeHelpers.GetHashCode(instance));
            }).Start();

            new Thread(() =>
            {
                ReservationSingleton instance = ReservationSingleton.GetInstance();
                Console.WriteLine("2nd thread instace ID " + RuntimeHelpers.GetHashCode(instance));
            }).Start();
        }