zoukankan      html  css  js  c++  java
  • [多线程] ManualResetEvent(线程之间通讯 通知或者查看是否完成)

    在别人的基础上修改成ManualResetEvent, 以取代suspend, resume. 

      public class Boy
        {
            private ManualResetEvent ent;
            public Boy(ManualResetEvent e)
            {
                ent = e;
            }
            public void SendFlower()
            {
                Console.WriteLine("正在送花的途中");
                for (int i = 0; i < 10; i++)
                {
                    Thread.Sleep(200);
                    Console.Write("..");
                }
                Console.WriteLine("花已经送到");
                ent.Set();
            }
        }

        class Program
        {
            private static ManualResetEvent ent = new ManualResetEvent(false);
            static void Main(string[] args)
            {
                Boy sender = new Boy(ent);
                for (int i = 0; i < 2; i++)
                {
                    Thread th = new Thread(new ThreadStart(sender.SendFlower));
                    th.Start();
                    //Console.WriteLine("-before wait?-" + ent.WaitOne(0)); //false
                    ent.WaitOne();
                    //Console.WriteLine("-before reset?-" + ent.WaitOne(0)); //true
                    ent.Reset();
                    //Console.WriteLine("-after reset?-" + ent.WaitOne(0)); //false
                    Console.WriteLine("收到了吧,花是我送嘀:)/r/n/r/n");
                }
                Console.ReadLine();
            }
        }

  • 相关阅读:
    css浏览器兼容问题集锦
    【转】H264编码原理以及I帧B帧P帧
    Makefile Shell 脚本;sed命令
    oProfile 学习
    C++ 局部变量的析构
    【转】C++ 单例模式
    C++ operator 知识点 2
    C++ operator 知识点
    218多校第九场 HDU 6424 (数学)
    2018多校第九场 HDU 6416 (DP+前缀和优化)
  • 原文地址:https://www.cnblogs.com/webglcn/p/2487462.html
Copyright © 2011-2022 走看看