zoukankan      html  css  js  c++  java
  • 线程同步中使用信号量AutoResetEvent

    using System;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var p = new Program();
                p.Do();
                p.Signal();
            }
    
            AutoResetEvent autoResetEvent = new AutoResetEvent(false); //false代表默认中阻塞状态
    
            void Do()
            {
                var worker = new Thread(() =>
                {
                    Console.WriteLine("Start worker");
                    Console.WriteLine("wait");
                    autoResetEvent.WaitOne(); //等待信号
                    Console.WriteLine("do");
                });
                worker.Start();
            }
    
            void Signal()
            {
                Console.WriteLine("Sent signal");
                Console.ReadKey();
                autoResetEvent.Set(); //发送信号
                Console.ReadKey();
            }
        }
    }

    ManualResetEvent需要手动设定阻塞状态设置为false

  • 相关阅读:
    git知识点总结
    自动化进阶
    unittest单元测试框架
    自动化测试模型
    webdriver
    python文件处理
    uva 11077 置换
    poj 1066 Treasure Hunt
    poj 2661 Factstone Benchmark
    hdu 4180
  • 原文地址:https://www.cnblogs.com/goodspeed/p/4068408.html
Copyright © 2011-2022 走看看