zoukankan      html  css  js  c++  java
  • 一个实例明白AutoResetEvent和 ManulResetEvent的用法

    先看一段代码:
    public class WaitHandlerExample { 
                    public static AutoResetEvent waitHandler; 
                    public static ManualResetEvent manualWaitHandler; 
     
                    public static void ThreadPoolMain() { 
                            waitHandler = new AutoResetEvent(false); 
                            manualWaitHandler = new ManualResetEvent(false); 
     
                            // Queue the task. 
                            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));             
                            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc2)); 
     
                            Console.WriteLine("Main thread does some work, then waiting...."); 
                            manualWaitHandler.WaitOne(); 
                            //waitHandler.Reset(); 
                            manualWaitHandler.WaitOne(); 
                            //waitHandler.Reset(); 
                            Console.WriteLine("Main thread exits."); 
                    } 
     
                    // This thread procedure performs the task. 
                    public static void ThreadProc(Object stateInfo) { 
                            Thread.Sleep(1000); 
                            Console.WriteLine("Hello from the thread pool."); 
                            //waitHandler.Set();        // 
                            manualWaitHandler.Set();//过去了,但是没关,也就是说 信号还是开着的。 
                            //manualWaitHandler.Reset(); 
                    } 
                    public static void ThreadProc2(object stateInfo) { 
                            Thread.Sleep(100); 
                            Console.WriteLine("Hello from the thread Pool2"); 
                            //waitHandler.Set(); 
                            manualWaitHandler.Set();//过去了,但是没有关 
                    } 
            }
    如果把 AutoResetEvent 比作 北京地铁的门闸,那么
    AutoResetEvent waitHandler=new AutoResetEvent(false);
    可以看作,初始化闸机口为关闭状态,
    waitHandler.WaitOne();
    可以看作刷卡
    waitHandler.Set()
    表示通过,并且闸机自动关闭(AutoReset)为下次通过做准备。他的一个重大的好处,就是线程只能一个一个通过,保持了顺序又避免了死锁。
     
    如果使用manualResetEvent 呢,那么在waitHandler.Set 之后,必须调用Reset()方法,为下面一位进去做好准备, 否则,就相当于无法再次刷卡。
     
    体现在程序中就是,AutoResetEvent 可以WaitOne 很多次,可是ManualResetEvent 如果不Reset 下次就不能使用这就是他们的区别。
  • 相关阅读:
    未能加载文件或程序集“xxx, Version=x.x.x.x, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx”或它的某一个依赖项。系统找不到指定的文件
    RabbitMQ本地正常,发布到服务器上 出行连接失败
    Windows 服务 创建 安装 卸载 和调试
    CSS 格式化 一行一条
    ES6---new Promise()讲解,Promise对象是用来干嘛的?
    Win Server 2008 R2 IIS 默认只能添加一个 443 HTTPS 端口
    MVC 部分视图:Partial() 、RenderPartial() 、 Action() 、RenderAction() 、 RenderPage() 区别
    ;(function ($, undefined){ })(jQuery); 的使用及说明
    JS中的call()方法和apply()方法用法总结
    MongoDB服务无法启动,windows提示发生服务特定错误:100
  • 原文地址:https://www.cnblogs.com/gjhjoy/p/3550136.html
Copyright © 2011-2022 走看看