using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
/*
 * by xiaoa_m
 * http://blog.csdn.net/xiaoa_m
 */
namespace ThreadRandom
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteClass wc = new WriteClass(200);
            RandomThread rt1 = new RandomThread("线程1",wc);
            RandomThread rt2 = new RandomThread("线程2",wc);
            RandomThread rt3 = new RandomThread("线程3",wc);            Thread t1 = new Thread(new ThreadStart(rt1.run));
            Thread t2 = new Thread(new ThreadStart(rt2.run));
            Thread t3 = new Thread(new ThreadStart(rt3.run));
            //t1.Priority = ThreadPriority.Lowest;
            //t2.Priority = ThreadPriority.Highest;
            t1.Start();
            t2.Start();
            t3.Start();
        }
    }
    class WriteClass
    {
        private int EnabledNum=0;
        private int n = 0;
        public WriteClass(int EnabledNum)
        {
            this.EnabledNum = EnabledNum;
        }
        public void write(string ThreadName,int r)
        {
            lock (this)
            {
                Console.WriteLine(ThreadName + "得到了随机数:" + r.ToString());
                n = n + 1;
                if (n > EnabledNum)
                {
                    Console.ReadKey();
                    Thread.CurrentThread.Abort();
           
                }
            }
        }
    }
    class RandomThread
    {
        private string ThreadName;
        private WriteClass wc;
        public RandomThread(string ThreadName,WriteClass wc)
        {
            this.ThreadName = ThreadName;
            this.wc = wc;
        }
        public void run()
        {
            while (true)
            {
                Random r = new Random();
                int r1 = r.Next(5);//返回一个小于5的非负整数
                if (r1 == 3)
                {
                    Console.WriteLine(this.ThreadName + "等待输出...");
                    wc.write(ThreadName, r1); 
                }
                try
                {
                    Thread.Sleep(0);
                }
                catch (ThreadInterruptedException) { }
            }
        }
    }
}
结果是:按道理,第一行应该是:线程1等待输出...。可是为什么没显示呢?百思不得其解。把Thread.Sleep(),改的大一点就显示了。难道是没执行?

解决方案 »

  1.   

    if (r1 == 3)
                    {
                        Console.WriteLine(this.ThreadName + "等待输出...");
                        wc.write(ThreadName, r1); 
                    }
    写这个程序我主要是看看会不会出现,2个线程同时等待的情况,但是没出现。
      

  2.   

    ding  ding  ding  ding  ding 
      

  3.   

    楼主是你自己没看清而已. 
    你觉得这样能看清么?把Program类改写成如下:
        class Program
        {
            public static StreamWriter sw;        static void Main(string[] args)
            {
                FileStream aFile = new FileStream("Log.txt", FileMode.Create);
                Program.sw = new StreamWriter(aFile);            WriteClass wc = new WriteClass(200);
                RandomThread rt1 = new RandomThread("线程1", wc);
                RandomThread rt2 = new RandomThread("线程2", wc);
                RandomThread rt3 = new RandomThread("线程3", wc);            Thread t1 = new Thread(new ThreadStart(rt1.run));
                Thread t2 = new Thread(new ThreadStart(rt2.run));
                Thread t3 = new Thread(new ThreadStart(rt3.run));
                //t1.Priority = ThreadPriority.Lowest;
                //t2.Priority = ThreadPriority.Highest;
                t1.Start();
                t2.Start();
                t3.Start();            while (t1.IsAlive || t2.IsAlive || t3.IsAlive) ;            Program.sw.Close();
            }
        }
    然后, 把其它类中的 Console.WriteLine 替换成 Program.sw.WriteLine
    程序运行结束后, 在可执行程序目录下去查看 Log.txt 文件. 里面有最终的显示结果. 
    跟你想的是一样的.