线程同步关于ManualResetEvent 知识
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Test
{
class Program
{
//默认信号为不发送状态
private static ManualResetEvent mre = new ManualResetEvent(false);
static void Main(string[] args)
{
EastWind wind = new EastWind(mre);
//启动东风的线程
Thread thd = new Thread(new ThreadStart(wind.WindComming));
thd.Start();
mre.WaitOne();//万事俱备只欠东风,事情卡在这里了,在东风来之前,诸葛亮没有进攻
//东风到了,可以进攻了
Console.WriteLine("诸葛亮大吼:东风来了,可以进攻了,满载燃料的大船接着东风冲向曹操的战船");
Console.ReadLine();
}
}
/// <summary>
/// 传说中的东风
/// </summary>
class EastWind
{
ManualResetEvent _mre;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="mre"></param>
public EastWind(ManualResetEvent mre)
{
_mre = mre;
}
/// <summary>
/// 风正在吹过来
/// </summary>
public void WindComming()
{
Console.WriteLine("东风正在吹过来");
for (int i = 0; i <= 5; i++)
{
Thread.Sleep(500);
Console.WriteLine("东风吹啊吹,越来越近了...");
}
Console.WriteLine("东风终于到了");
//通知诸葛亮东风已到,可以进攻了,通知阻塞的线程可以继续执行了
_mre.Set();
}
}
}
想请问大神,既然是线程同步执行,那么“ mre.WaitOne();”加不加对输出结果应该是不影响的吧,因为同步的话只能thd这个线程执行完才能执行主线程也就是 Console.WriteLine("诸葛亮大吼:东风来了,可以进攻了,满载燃料的大船接着东风冲向曹操的战船");,我理解的对么??