首页 新闻 搜索 专区 学院

帮我解释下这段代码

0
悬赏园豆:10 [已关闭问题] 关闭于 2009-11-21 18:56

using System;
using System.Threading;
using System.Collections;

namespace MonitorCS1
{
        class MonitorSample
        {
                const int MAX_LOOP_TIME = 10;
                Queue m_smplQueue;
               
                public MonitorSample()
                {
                        m_smplQueue = new Queue();
                }
                public void FirstThread()
                {
                        int counter = 0;
                        lock (m_smplQueue)
                        {
                                while (counter < MAX_LOOP_TIME)
                                {
                                        //Wait, if the queue is busy.
                                        Monitor.Wait(m_smplQueue);
                                        //Push one element.
                                        m_smplQueue.Enqueue(counter);
                                        //Release the waiting thread.
                                        Monitor.Pulse(m_smplQueue);

                                        counter++;
                                }
                        }
                }
                public void SecondThread()
                {
                        lock (m_smplQueue)
                        {
                                //Release the waiting thread.
                                Monitor.Pulse(m_smplQueue);
                                //Wait in the loop, while the queue is busy.
                                //Exit on the time-out when the first thread stops.
                                while (Monitor.Wait(m_smplQueue, 1000))
                                {
                                        //Pop the first element.
                                        int counter = (int)m_smplQueue.Dequeue();
                                        //Print the first element.
                                        Console.WriteLine(counter.ToString());
                                        //Release the waiting thread.
                                        Monitor.Pulse(m_smplQueue);
                                }
                        }
                }
                //Return the number of queue elements.
                public int GetQueueCount()
                {
                        return m_smplQueue.Count;
                }

                static void Main(string[] args)
                {
                        //Create the MonitorSample object.
                        MonitorSample test = new MonitorSample();
                        //Create the first thread.
                        Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
                        //Create the second thread.
                        Thread tSecond = new Thread(new ThreadStart(test.SecondThread));
                        //Start threads.
                        tFirst.Start();
                        tSecond.Start();
                        //wait to the end of the two threads
                        tFirst.Join();
                        tSecond.Join();
                        //Print the number of queue elements.
                        Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString());
                }
        }
}

问题补充: 其实这段代码是msdn中关于Monitor.Pulse方法的例子。 请各位大侠详细解释下: Monitor.Wait,Monitor.Pulse,Monitor.PulseAll 的使用,谢谢!!!
xiaoweiz的主页 xiaoweiz | 初学一级 | 园豆:130
提问于:2009-11-08 11:32
< >
分享
所有回答(2)
0

上面的注释挺完整的,从字面上理解,wait就是让正在执行的线程进入暂停(挂起)状态,pulse就是让暂停的线程继续执行,pulseAll就是释放所有被挂起的线程。

dege301 | 园豆:2825 (老鸟四级) | 2009-11-08 12:14
0

一楼

JieNet | 园豆:0 (初学一级) | 2009-11-08 12:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册