C#多线程编程之:同步事件和等待句柄2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Calculate2 calc = new Calculate2();

            Label3.Text = calc.Result().ToString();
        }
    }

    class Calculate
    {
        //基数、数1、数2、数3
        double baseNumber, firstTerm, secondTerm, thirdTerm;
        //自动重置同步事件数组
        AutoResetEvent[] autoEvents;
        //手动重置同步事件
        ManualResetEvent manualEvent;
        //随机数生成器
        Random randomGenerator;

        public Calculate()
        {
            //初始化同步事件
            autoEvents = new AutoResetEvent[]
                     {
                         new AutoResetEvent(false), 
                         new AutoResetEvent(false), 
                       new AutoResetEvent(false
                     };

            manualEvent = new ManualResetEvent(false);
        }

        void CalculateBase(object stateInfo)
        {
            baseNumber = randomGenerator.NextDouble();

            //置事件状态为终止状态,使等待线程继续
            manualEvent.Set();
        }

        void CalculateFirstTerm(object stateInfo)
        {
            double preCalc = randomGenerator.NextDouble();
            //等待手动重置事件终止
            manualEvent.WaitOne();
            firstTerm = preCalc * baseNumber * randomGenerator.NextDouble();
            //置自动事件终止信号
            autoEvents[0].Set();
        }

        void CalculateSecondTerm(object stateInfo)
        {
            double preCalc = randomGenerator.NextDouble();
            Thread.Sleep(30000);
            //等待手动重置事件终止
            manualEvent.WaitOne();
            secondTerm = preCalc * baseNumber * randomGenerator.NextDouble();
            //置自动事件终止信号
            autoEvents[1].Set();
        }

        void CalculateThirdTerm(object stateInfo)
        {
            double preCalc = randomGenerator.NextDouble();
            //等待手动重置事件终止
            manualEvent.WaitOne();
            thirdTerm = preCalc * baseNumber * randomGenerator.NextDouble();
            //置自动事件终止信号
            autoEvents[2].Set();
        }

        public double Result(int seed)
        {
            randomGenerator = new Random(seed);

            //将待执行工作加入线程
            
//ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateBase));
            
//ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateFirstTerm));
            
//ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateSecondTerm));
            
//ThreadPool.QueueUserWorkItem(new WaitCallback(CalculateThirdTerm));

            Thread ThreadBase = new Thread(CalculateBase);
            ThreadBase.Start();

            Thread ThreadFirst = new Thread(CalculateFirstTerm);
            ThreadFirst.Start();

            Thread ThreadSecond = new Thread(CalculateSecondTerm);
            ThreadSecond.Start();

            Thread ThreadThird = new Thread(CalculateThirdTerm);
            ThreadThird.Start();

            //等待所有自动事件终止
            WaitHandle.WaitAll(autoEvents);
            //重置手动事件为非终止状态
            manualEvent.Reset();

            return firstTerm + secondTerm + thirdTerm;
        }
    }

    class Calculate2
    {
        double baseNumber, totalNumber;
        List<AutoResetEvent> autoEvents;
        ManualResetEvent manualEvent;
        Random randomGenerator;

        public Calculate2()
        {
            //初始化同步事件
            autoEvents = new List<AutoResetEvent>();

            for (int i = 0; i < 3; i++)
            {
                autoEvents.Add(new AutoResetEvent(false));
            }

            manualEvent = new ManualResetEvent(false);
        }

        void CalculateBase()
        {
            baseNumber = 0;// new Random().Next();

            
//置事件状态为终止状态,使等待线程继续
            manualEvent.Set();
        }

        void CalculateTerm(object i)
        {
            int j = (int)i;
            //等待手动重置事件终止
            manualEvent.WaitOne();
            lock (this)
            {
                totalNumber += j + baseNumber;
            }
            //置自动事件终止信号
            autoEvents[j].Set();
        }

        public double Result()
        { 
            Thread ThreadBase = new Thread(CalculateBase);
            ThreadBase.Start();

            for (int i = 0; i < 3; i++)
            {
                Thread ThreadFirst = new Thread(new ParameterizedThreadStart(CalculateTerm));
                ThreadFirst.Start(i);
            }

            //等待所有自动事件终止
            WaitHandle.WaitAll(autoEvents.ToArray());
            //重置手动事件为非终止状态
            manualEvent.Reset();

            return totalNumber;
        }

    }
}
posted on 2012-02-08 16:55  罗里罗嗦夫斯基  阅读(589)  评论(0编辑  收藏  举报