解决方案 »

  1.   

    using System;
    using System.Threading;namespace ConsoleApplication1
    {
        class Program
        {
            static Mutex mutex = new Mutex();
            static number num = new number();
            class number
            {
                public int a = 0;
            }
            class Plus
            {
                public void plus()
                {
                    while (true)
                    {
                        mutex.WaitOne();
                        num.a++;
                        Console.WriteLine("OK");
                        mutex.ReleaseMutex();
                        Thread.Sleep(500);
                    }
                }
            }
            class Show
            {
                public void show()
                {
                    while (true)
                    {
                        mutex.WaitOne();
                        Console.WriteLine(num.a);
                        mutex.ReleaseMutex();
                        Thread.Sleep(500);
                    }
                }
            }
            static void Main(string[] args)
            {
                Plus plus = new Plus();
                Show show = new Show();
                Thread thread1 = new Thread(new ThreadStart(plus.plus));
                Thread thread2 = new Thread(new ThreadStart(show.show));
                thread1.Start();
                thread2.Start();
                new ManualResetEvent(false).WaitOne();      //按Ctrl+C退出
            }    }
    }
      

  2.   

    实际上,如果你把第二个  Thread.Sleep(500); 语句删除掉,你会发现这个程序输出的是垃圾信息(根本没有保持现在的输出序列)!因此,我不知道你这个程序是从哪里抄来的,但是要告诉你,稍微变换一下测试方法,你应该发现哪些代码是垃圾。不要轻信一些博客、培训师所给你的代码。你要自己动手测试!
      

  3.   

    这样的同步才是正常的using System;
    using System.Threading;namespace ConsoleApplication1
    {
        class Program
        {
            static AutoResetEvent 生产 = new AutoResetEvent(true);
            static AutoResetEvent 使用 = new AutoResetEvent(false);
            static number num = new number();        class number
            {
                public int a = 0;
            }        class Plus
            {
                public void plus()
                {
                    while (true)
                    {
                        生产.WaitOne();
                        num.a++;
                        Console.WriteLine("OK");
                        使用.Set();
                        Thread.Sleep(500);
                    }
                }
            }        class Show
            {
                public void show()
                {
                    while (true)
                    {
                        使用.WaitOne();
                        Console.WriteLine(num.a);
                        生产.Set();
                    }
                }
            }
            static void Main(string[] args)
            {
                Plus plus = new Plus();
                Show show = new Show();
                Thread thread1 = new Thread(new ThreadStart(plus.plus));
                Thread thread2 = new Thread(new ThreadStart(show.show));
                thread1.Start();
                thread2.Start();
                new ManualResetEvent(false).WaitOne();      //按Ctrl+C退出
            }    }
    }