送花同步:AutoResetEvent=ManualResetEvent(Set+Reset)

来源:互联网 发布:二级c语言培训班 编辑:程序博客网 时间:2024/04/27 17:33

using System;

using System.Threading;


public class TestMain
{
    private static AutoResetEvent ent = new AutoResetEvent(false);
    public static void Main()
    {
        Boy sender = new Boy(ent);
       for (int i = 0; i < 3; i++)
        {

            Thread th = new Thread(new ThreadStart(sender.SendFlower));

            th.Start();

            ent.WaitOne();

            Console.WriteLine("收到了吧,花是我送嘀:)/r/n/r/n");

        }
        Console.ReadLine();
    }
}
public class Boy
{
    AutoResetEvent ent;
    public Boy(AutoResetEvent e)
    {

        ent = e;

    }
    public void SendFlower()
    {

        Console.WriteLine("正在送花的途中");
        for (int i = 0; i < 10; i++)
        {

            Thread.Sleep(200);

            Console.Write("..");

        }

        Console.WriteLine("/r/n花已经送到MM手中了,boss");
        ent.Set();

    }

}

原创粉丝点击