多线程学习笔记之ManualResetEvent(二)(转载)

问题:如果需求有变化,比如要求在主线程执行到某个特定的位置(或时间点)时,才让其它线程开始介入,该怎么做呢?(这种情况实际中很常见,比如某一项计算的入口参数依赖于另一项计算的结果,再比如我们计算月工资前,得先统计出员工当月考勤情况)

System.Threading命名空间下有一个ManualResetEvent类,可以做到这一点:
using System;
using System.Threading;

namespace ManualResetEventStudy
{

    
class ThreadClass
    {       

        
static ManualResetEvent mre = new ManualResetEvent(false);

        
static void t1()
        {
            
mre.WaitOne(1000);//等待1秒后,自行启动
            for (int x = 1; x <= 5; x++)
            {
                Thread.Sleep(
500);
                Console.WriteLine(
"t1的x:" + x);
            }
        }

        
static void t2()
        {
            
mre.WaitOne();//一直等待下去,直到有"人"调用mre.set()发出信号为止
            for (int x = 1; x <= 5; x++)
            {
                Thread.Sleep(
500);
                Console.WriteLine(
"t2的x:" + x);
            }
        }

        
static void Main(string[] args)
        {
            Thread thrd1 
= new Thread(t1);
            thrd1.Start();

            Thread thrd2 
= new Thread(t2);
            thrd2.Start();


            
for (int x = 1; x <= 5; x++)
            {
                Thread.Sleep(
500);
                Console.WriteLine(
"主线程中的x:" + x);

                if (x == 3
                {
                    mre.Set();//通知所有等待的线程:“同志们,可以动啦”:)
                }
            }

            Console.Read();
        }        

    }
}

 t1方法中,我们用 mre.WaitOne(1000);让调用该方法的线程先等候1秒,t2方法中,我们用mre.WaitOne()无限等候,然后主线程中计数到3的时候,手动调用mre.Set()方法唤醒所有等候中的线程,运行结果类似下面这样:

主线程中的x:1
主线程中的x:2
t1的x:1
主线程中的x:3
t1的x:2
t2的x:1
主线程中的x:4
t1的x:3
主线程中的x:5
t2的x:2
t1的x:4
t2的x:3
t1的x:5
t2的x:4
t2的x:5

转自:http://www.cnblogs.com/yjmyzz/archive/2009/11/26/1611268.html
原文地址:https://www.cnblogs.com/johnwonder/p/1676782.html