C#中多线程ManualResetEvent 与 AutoResetEvent 区别

所属分类: 软件编程 / C#教程 阅读数: 88
收藏 0 赞 0 分享

在多线程开发中,时常用到ManualResetEvent 与AutoResetEvent 。 它们如同道路交通中的信号灯。两者之间有什么区别呢?

共同点:

均继承EventWaitHandle 接口,因此,均具有以下功能:
Reset() //红灯
Set() //绿灯
WaitOne() // 等待信号

不同点:

AutoResetEvent 收到 Set 后 , 一次只能执行一个线程,其它线程继续 WaitOne 。
ManualResetEvent 收到 Set 后,所有处理 WaitOne 状态线程均继续执行。

msdn 提到(如果没有线程 处于WaitOne() 状态,而调用 Set ,AutoResetEvent将保持Set 状态):

调用Set信号AutoResetEvent释放等待线程。 AutoResetEvent 将保持终止状态直到一个等待线程释放,并自动返回到非信号状态。 如果没有线程处于等待状态,状态将无限期地保持已发出信号。

因此通常WatiOne 之前,先 Reset() 一下,清除Set 信号

需要注意的是(两个 Set 调用之间时间较短,第二个 Set 信号可能会丢失,因此连续 Set 调用,中间需要 Sleep 一定时间):

不能保证的每个调用Set方法将释放一个线程。 如果两次调用太靠近在一起,以便第二次调用前释放线程发生,只有一个线程被释放。 就像第二次调用未发生。 此外,如果Set时没有等待的线程调用和AutoResetEvent已终止,则调用不起作用。

有网友说:

AutoResetEvent.Set() = ManualResetEvent.Set() + ManualResetEvent.Reset();

个人理解 ,这只是原理层面含义,实际使用过程中,有差别的,如下示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testManualResetEvent
{
  class Program
  {
    static object objManualResetEvent = new object();
    static System.Threading.ManualResetEvent manu = new System.Threading.ManualResetEvent(false);
     //static System.Threading.AutoResetEvent manu = new System.Threading.AutoResetEvent(false);
    static void Main(string[] args)
    {
      
      for (int i = 0; i < 10; i++)
      {
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(() => { Product(); }));
        t.Start();
      }
      
      manu.Set();
      manu.Reset();

      Console.ReadKey();
    }

    static void Product()
    {
      manu.WaitOne(10000);
      Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
    }
  }
}

实际执行结果 , 在 执行 set 后 reset 前 ,有多少个线程唤起执行,无法预料:

需要加锁 ,确保一次通过一个线程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testManualResetEvent
{
  class Program
  {
    static object objManualResetEvent = new object();
    static System.Threading.ManualResetEvent manu = new System.Threading.ManualResetEvent(false);
     //static System.Threading.AutoResetEvent manu = new System.Threading.AutoResetEvent(false);
    static void Main(string[] args)
    {
      
      for (int i = 0; i < 10; i++)
      {
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(() => { Product(); }));
        t.Start();
      }
      
      manu.Set();

      //System.Threading.Thread.Sleep(100); //连续 set 需要 sleep
      //manu.Set();
      //manu.Reset();

      //System.Threading.Thread.Sleep(100);
      //manu.Set();
      //manu.Reset();

      Console.ReadKey();
    }

    static void Product()
    {
      lock (objManualResetEvent)
      {
        manu.WaitOne(10000);          manu.Reset();
        Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
      }
    }
  }
}

执行结果:

更多精彩内容其他人还在看

WPF仿三星手机充电界面实现代码

这篇文章主要为大家详细介绍了WPF仿三星手机充电界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

浅谈C#各种数组直接的数据复制/转换

下面小编就为大家带来一篇浅谈C#各种数组直接的数据复制/转换。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C#访问SQLServer增删改查代码实例

这篇文章主要为大家详细介绍了C#访问SQLServer增删改查代码实例,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#根据身份证号码判断出生日期和性别

这篇文章主要为大家详细介绍了C#根据身份证号码判断出生日期和性别的方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C# 向Word中设置/更改文本方向的方法(两种)

在一般情况下word中输入的文字都是横向的,今天小编给大家带来两种方法来设置更改文本方向的方法,非常不错,对c# word 更改文本方向的知识感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

让C# Excel导入导出 支持不同版本Office

让C# Excel导入导出,支持不同版本的Office,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#设置自定义文件图标实现双击启动(修改注册表)

这篇文章介绍的是利用C#设置自定义文件图标,然后实现双击启动的功能,文章给出了示例代码,介绍的很详细,有需要的可以参考借鉴。
收藏 0 赞 0 分享

C#两个相同属性的类赋值方法

这篇文章主要介绍了C#两个相同属性的类赋值方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

C#中ListView控件实现窗体代码

这篇文章主要介绍了C#中ListView控件实现窗体的核心代码,非常不错,具有参考借鉴价值,对c#listview相关知识感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

浅谈C# 序列化与反序列化几种格式的转换

下面小编就为大家带来一篇浅谈C# 序列化与反序列化几种格式的转换。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多