コード例 #1
0
        public void TestMethod1()
        {
            Singleton.ThreadSingleton instance1 = null;
            Singleton.ThreadSingleton instance2 = null;
            Thread t1 = new Thread(() => instance1 = Singleton.ThreadSingleton.Instance);
            Thread t2 = new Thread(() => instance2 = Singleton.ThreadSingleton.Instance);

            t1.Start(); t2.Start();
            t1.Join(); t2.Join();

            Assert.AreNotSame(instance1, instance2);
        }
コード例 #2
0
        public void ThreadSingletonWithThreads()
        {
            // This time we expect each thread has its own instance of a singleton,
            // thus we want singleton1 to be different than singleton2.
            Singleton.ThreadSingleton singleton1 = null;
            Singleton.ThreadSingleton singleton2 = null;

            var thread1 = new Thread(() => singleton1 = Singleton.ThreadSingleton.Instance());
            var thread2 = new Thread(() => singleton2 = Singleton.ThreadSingleton.Instance());

            thread1.Start();
            thread2.Start();

            thread1.Join();
            thread2.Join();

            Assert.That(singleton1, Is.Not.EqualTo(singleton2));
        }