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

    只放任一个线程。

    更多:

  • 相关阅读:
    splice() 的用法
    JAVA 序列化
    对象的组合
    基于SSM框架实现简单的登录注册
    SpringMVC 处理映射
    基于高德地图的位置服务开发(二)
    基于高德地图的位置服务开发(一)
    Spring MVC页面重定向
    Spring MVC
    Spring MVC
  • 原文地址:https://www.cnblogs.com/pylblog/p/12887494.html
Copyright © 2011-2022 走看看