Exemplo n.º 1
0
        public static void Main()
        {
            Logger obj1 = Logger.GetInstance();
            Logger obj2 = Logger.GetInstance();

            Test newObj1 = new Test();
            Test newObj2 = new Test();

            //threadSafe
            var safelogger  = ThreadSafeLogger.GetInstance();
            var safelogger2 = ThreadSafeLogger.GetInstance();


            //will display same hash because only one instance is created
            Console.WriteLine(safelogger.GetHashCode());
            Console.WriteLine(safelogger2.GetHashCode());

            //will display same hash because only one instance is created
            Console.WriteLine(obj1.GetHashCode());
            Console.WriteLine(obj2.GetHashCode());

            //will display different hash as different instances has been created

            Console.WriteLine(newObj1.GetHashCode());
            Console.WriteLine(newObj2.GetHashCode());
        }
Exemplo n.º 2
0
        public static ThreadSafeLogger GetInstance()
        {
            if (_safeLogger == null)
            {
                lock (_padlock)
                {
                    if (_safeLogger == null)
                    {
                        _safeLogger = new ThreadSafeLogger();
                    }
                }
            }

            return(_safeLogger);
        }