示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Incorrect counter");

            var c = new Counter();

            var t1 = new Thread(() => TestCounter(c));
            var t2 = new Thread(() => TestCounter(c));
            var t3 = new Thread(() => TestCounter(c));

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

            Console.WriteLine("Total count: {0}", c.Count);
            Console.WriteLine("--------------------------");

            Console.WriteLine("Correct counter");

            var c1 = new CounterWithLock();

            t1 = new Thread(() => TestCounter(c1));
            t2 = new Thread(() => TestCounter(c1));
            t3 = new Thread(() => TestCounter(c1));
            t1.Start();
            t2.Start();
            t3.Start();
            t1.Join();
            t2.Join();
            t3.Join();
            Console.WriteLine("Total count: {0}", c1.Count);
        }
示例#2
0
        /// <summary>
        /// 使用lock锁
        /// </summary>
        public static void LockThread()
        {
            Console.WriteLine("Incorrect Counter");
            Counter c1 = new Counter();
            var     t1 = new Thread(() => TestCounter(c1));
            var     t2 = new Thread(() => TestCounter(c1));
            var     t3 = new Thread(() => TestCounter(c1));

            t1.Start();
            t2.Start();
            t3.Start();
            t1.Join();
            t2.Join();
            t3.Join();
            Console.WriteLine($"Total Count: {c1.Count}");
            Console.WriteLine("------------------------");

            Console.WriteLine("Correct counter");
            CounterWithLock c2 = new CounterWithLock();

            t1 = new Thread(() => TestCounter(c2));
            t2 = new Thread(() => TestCounter(c2));
            t3 = new Thread(() => TestCounter(c2));
            t1.Start();
            t2.Start();
            t3.Start();
            t1.Join();
            t2.Join();
            t3.Join();
            Console.WriteLine($"Total count:{c2.Count}");
            Console.ReadLine();
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("Incorrect counter");

            var counter = new Counter();

            var workThread1 = new Thread(() => TestCounter(counter));
            var workThread2 = new Thread(() => TestCounter(counter));
            var workThread3 = new Thread(() => TestCounter(counter));

            workThread1.Start();
            workThread2.Start();
            workThread3.Start();

            workThread1.Join();
            workThread2.Join();
            workThread3.Join();

            Console.WriteLine("Total count: {0}", counter.Count);
            Console.WriteLine("--------------------------");
            Console.WriteLine("Correct counter");

            var counterLocked = new CounterWithLock();

            var workThread4 = new Thread(() => TestCounter(counterLocked));
            var workThread5 = new Thread(() => TestCounter(counterLocked));
            var workThread6 = new Thread(() => TestCounter(counterLocked));

            workThread4.Start();
            workThread5.Start();
            workThread6.Start();

            workThread4.Join();
            workThread5.Join();
            workThread6.Join();

            Console.WriteLine("Total Count : {0}", counterLocked.Count);

            Console.ReadKey();
        }