zoukankan      html  css  js  c++  java
  • ManualResetEvent使用

    假设一个使用场景:

    1. 有一台仪器,需要预热后才能使用;

    2. 需要使用线程操作这台仪器;

    static bool isReady = false;
            public static void Test1()
            {
                Thread th = new Thread(() =>
                {
                    while (!isReady) ; //不断执行此语句
                    Console.WriteLine("开始工作");
                });
                th.Start();
                Console.WriteLine("按任意键继续...");
                Console.ReadKey();
                isReady = true; 
                Console.ReadKey();
            }

    使用ManualResetEvent后:

     //注意到这里设置为false,
            static ManualResetEvent manual = new ManualResetEvent(false);
            public static void Test1()
            {
                Thread th = new Thread(() =>
                {
                    manual.WaitOne();
                    Console.WriteLine("开始工作");
                });
                th.Start();
                Console.WriteLine("按任意键继续...");
                Console.ReadKey();
                manual.Set();
                Console.ReadKey();
            }

    性能差距居然如此巨大!

    再看一下,改为:

    static ManualResetEvent manual = new ManualResetEvent(true);

    manual.WaitOne(); //这句不再进行阻塞了

    也就是说:

    static ManualResetEvent manual = new ManualResetEvent(false); //代表一开始要进行阻塞,相当于 isReady = false
    manual.Set(); //相当于isReady = true
    manual.WaitOne(); //while(!isReady);

    MSDN详解:  https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.manualresetevent?redirectedfrom=MSDN&view=netframework-4.8

    还有: manual.Reset(); ,相当于又  isReady = false;

    对应还有

    AutoResetEvent 类

    MSDN详解: https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.autoresetevent?view=netframework-4.8

    就是在manual.WaitOne()之后,自动manual.Reset()

    static ManualResetEvent manual = new ManualResetEvent(false);
            public static void Test1()
            {
                for (int i = 0; i < 5; i++)
                {
                    Thread th = new Thread(() =>
                    {
                        Console.WriteLine("线程" + Thread.CurrentThread.Name + "等待");
                        manual.WaitOne();
                        Console.WriteLine("线程" + Thread.CurrentThread.Name + "开始工作");
                    });
                    th.Name = i.ToString();
                    th.Start();
                }
                Console.WriteLine("按回车键继续...");
                Console.ReadLine();
                manual.Set();
                Console.ReadKey();
            }

     在ManualResetEvent时,一旦执行Set()后,所有的WaitOne()都掠过了;

    改成:static AutoResetEvent manual = new AutoResetEvent(false);

    只放任一个线程。

    更多:

  • 相关阅读:
    iOS 添加微信分享
    IOS学习笔记——ViewController生命周期详解
    UI之CALayer详解(转)
    iOS CALayer讲解
    [leetcode] Minimum Path Sum
    [leetcode]Binary Tree Maximum Path Sum
    我是真的想去google啊
    C++ string 用法详解
    继续存博客
    bash中 2>&1 & 的解释
  • 原文地址:https://www.cnblogs.com/pylblog/p/12887494.html
Copyright © 2011-2022 走看看