public static SingletonNotThreadSafe GetInstance(string value)
        {
            if (instance == null)
            {
                if (firstThread)
                {
                    firstThread = false;

                    //no locking means there is a chance two threads can access this code simultaneously
                    //simulating this chance by sleeping the first thread
                    Thread.Sleep(500);
                }
                instance       = new SingletonNotThreadSafe();
                instance.Value = value;
            }
            return(instance);
        }
示例#2
0
        public static void TestNotThreadSafeSingleton(string value)
        {
            var singleton = SingletonNotThreadSafe.GetInstance(value);

            Console.WriteLine(singleton.Value);
        }