日期:2014-05-17  浏览次数:20729 次

Monitor wait 和 Pulse问题
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
    class MonitorSample
    {
        const int MAX_LOOP_TIME = 1000;
        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()
        {