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();
            }
        }

  • 相关阅读:
    LinkedList实现原理(JDK1.8)
    ArrayList实现原理(JDK1.8)
    java集合讲解
    MySQL系列:MySQL的基本使用
    MySQL系列:一句SQL,MySQL是怎么工作的?
    MySQL系列:走进数据库,相关概念你都明白吗?
    MySQL系列:Windows 下 MySQL 8.X 的安装
    SpringBoot系列:Spring Boot集成定时任务Quartz
    SpringBoot系列:Spring Boot定时任务Spring Schedule
    SpringBoot系列:Spring Boot异步调用@Async
  • 原文地址:https://www.cnblogs.com/webglcn/p/2487462.html
Copyright © 2011-2022 走看看