首页 > 其他分享 >AutoResetEvent/ManualResetEvent 的简单理解与运用

AutoResetEvent/ManualResetEvent 的简单理解与运用

时间:2023-03-26 13:46:16浏览次数:39  
标签:Console Thread AutoResetEvent ManualResetEvent void static new 运用

AutoResetEvent 和 ManualResetEvent 只是构造函数包装器 它们唯一要做的就是使用EventResetMode.AutoReset或EventResetMode.ManualReset从EventWaitHandle调用构造函数.
三.常用方法
Reset ()
将事件状态设置为非终止状态,导致线程阻止;如果该操作成功,则返回true;否则,返回false.
Set ()
将事件状态设置为终止状态,允许一个或多个等待线程继续;如果该操作成功,则返回true;否则,返回false.

   注:不能保证每次调用 Set 该方法都会释放线程。 如果两个调用太接近,以便第二次调用在释放线程之前发生,则只释放一个线程。 就好像第二次调用没有发生一样。 此外,如果没有 Set 等待的线程且 AutoResetEvent 已发出信号,则调用无效。
WaitOne()
阻止当前线程,直到收到信号.
WaitOne(TimeSpan, Boolean)
阻止当前线程,直到当前实例收到信号,使用 TimeSpan 度量时间间隔并指定是否在等待之前退出同步域.

区别

.AutoResetEvent 和 ManualResetEvent都继承自EventWaitHandle.


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

class Program {
    static AutoResetEvent are;
    static void FinishOrTimeOut(object obj) {
        are.WaitOne();
        for(int i = 0; i < 500; i++) {
            Console.Write(i + " ");
        }
    }
    static void FinishOrTimeOut1(object obj) {
        are.WaitOne();
        for(int i = 500; i < 1000; i++) {
            Console.Write(i + " ");
        }
    }
    static void Main(string[] args) {
        using(are = new AutoResetEvent(false)) {
            Thread thread = new Thread(FinishOrTimeOut);
            thread.Start();
 
            Thread thread1 = new Thread(FinishOrTimeOut1);
            thread1.Start();
 
            Console.WriteLine("-----");
            are.Set();
 
            Console.ReadLine();
        }
    }
}

ManualResetEvent

class Program {
    static ManualResetEvent are;
    static void FinishOrTimeOut(object obj) {
        are.WaitOne();
        for(int i = 0; i < 500; i++) {
            Console.Write(i + " ");
        }
    }
    static void FinishOrTimeOut1(object obj) {
        are.WaitOne();
        for(int i = 500; i < 1000; i++) {
            Console.Write(i + " ");
        }
    }
    static void Main(string[] args) {
        using(are = new ManualResetEvent(false)) {
            Thread thread = new Thread(FinishOrTimeOut);
            thread.Start();
 
            Thread thread1 = new Thread(FinishOrTimeOut1);
            thread1.Start();
 
            Console.WriteLine("-----");
            are.Set();
 
            Console.ReadLine();
        }
    }
}

 

 

 

 

AutoResetEvent   自动Reset().
ManualResetEvent  手动调用Reset().

class Program {
    static ManualResetEvent are;
    //未被阻塞
    static void FinishOrTimeOut(object obj) {
        for(int i = 0; i < 500; i++) {
            Console.Write(i + " ");
        }
 
    }
    //被阻塞
    static void FinishOrTimeOut1(object obj) {
        are.WaitOne();
        for(int i = 500; i < 1000; i++) {
            Console.Write(i + " ");
        }
    }
    //被阻塞
    static void FinishOrTimeOut2(object obj) {
        are.WaitOne();
        for(int i = 1000; i < 1500; i++) {
            Console.Write(i + " ");
        }
    }
    static void Main(string[] args) {
        using(are = new ManualResetEvent(false)) {
            Thread thread = new Thread(FinishOrTimeOut);
            thread.Start();
 
            Thread thread1 = new Thread(FinishOrTimeOut1);
            thread1.Start();
 
            are.Set();
            Console.WriteLine("-----");
            Thread.Sleep(1000);
 
            Thread thread2 = new Thread(FinishOrTimeOut2);
            thread2.Start();
 
 
            Console.ReadLine();
        }
    }
}

 

class Program {
    static ManualResetEvent are;
    //未被阻塞
    static void FinishOrTimeOut(object obj) {
        for(int i = 0; i < 500; i++) {
            Console.Write(i + " ");
        }
 
    }
    //被阻塞
    static void FinishOrTimeOut1(object obj) {
        are.WaitOne();
        for(int i = 500; i < 1000; i++) {
            Console.Write(i + " ");
        }
        //手动关闭
        are.Reset();
    }
    //被阻塞
    static void FinishOrTimeOut2(object obj) {
        are.WaitOne();
        for(int i = 1000; i < 1500; i++) {
            Console.Write(i + " ");
        }
    }
    static void Main(string[] args) {
        using(are = new ManualResetEvent(false)) {
            Thread thread = new Thread(FinishOrTimeOut);
            thread.Start();
 
            Thread thread1 = new Thread(FinishOrTimeOut1);
            thread1.Start();
 
            are.Set();
            Console.WriteLine("-----");
            Thread.Sleep(1000);
 
            Thread thread2 = new Thread(FinishOrTimeOut2);
            thread2.Start();
 
 
            Console.ReadLine();
        }
}
}

1.Monitor是一个静态类,没有父类.
   AutoResetEvent 是密封类,继承EventWaitHandle->WaitHandle->MarshalByRefObject .

2.AutoResetEvent是传统的Win32 同步方式的封装,和使用传统的方式没有太大的区别,互斥体,信号量,事件等都是系统资源,有一定的性能开销,比Monitor性能要差一些.

3.Monitor只能在单一进程内使用.
而传统的同步机制互斥体:Mutex 信号量:Semaphore/SemaphoreSlim
事件:ManualResetEventSlim/CountdownEvent/AutoResetEvent/ManualResetEvent
是可以对多个进程进行同步的!(利用基类EventWaitHandle中的静态方法OpenExisting)

4.使用Monitor时可以保护资源.
AutoResetEvent 只是通知事情发生.
————————————————
版权声明:本文为CSDN博主「一梭键盘任平生」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/SmillCool/article/details/127241363


 

标签:Console,Thread,AutoResetEvent,ManualResetEvent,void,static,new,运用
From: https://www.cnblogs.com/l1pe1/p/17258552.html

相关文章

  • 运用物联网手段打造烟叶养护智慧管理和实时监测系统
    烟叶醇化库作为烟叶的主要储存环境,储存仓库的管理直接决定卷烟成品的品质。客房环境温湿度管控与烟叶霉变、碳化预防、虫情灾害直接影响到烟叶原料的醇化质量,而人工醇化速度......
  • 框架中处处可见反射的运用,你对它了解多少?
    什么是反射反射是一种能够在程序运行时动态访问、修改某个类中任意属性(状态)和方法(行为)的机制(包括private实例和方法),java反射机制提供了以下几个功能:在运行时判断任意一......
  • Socket 中运用 BufferedStream 类(转载)
    下面的代码示例演示如何使用 BufferedStream 类,而使用 NetworkStream 类来提高某些I/O操作的性能。在启动客户端之前,在远程计算机上启动服务器。启动客户端时,将远......
  • Pinia 快速上手运用
    相比于Vuex4.X而言Pinia可以很好的区分相应的Hooks不必要像Vuex一样先调用useStore获取我们的Store。同时去掉了Mutations属性,正常开发中其实这个属性非常鸡肋。两者相......
  • (五)博客园美化(风格1):音乐插件等小模块的运用
    空闲的时候自己根据很多大神的美化教程,把自己的博客园简单装修了下。再此整理一下美化方式和步骤,如果喜欢本人博客这种风格,可以参考一下这个系列。一、ForkmeonGitee......
  • 微商城系统开发应该运用什么招数?四个招数很管用
     如今想要做微商城系统开发的企业是非常多的,但是要想成功开发微商城,我们还要运用一些招数。那么微商城系统开发应该运用什么招数?今天名锐迅动为大家介绍四个招数很管用。......
  • 前端学习案例6-数组方法得总结和运用6
     ......
  • 秒杀面试题!JS中this指向的理解和运用
     1.引言本文旨在讲解JavaScript中的this指向的概念和运用,帮助前端开发者更好地理解和应用this关键字。 2.this的概念在JavaScript中,this是一个关键字,用于指向当前......
  • 2.熟练运用es5、es6提供的语法规范(推荐)
    ECMAScript(核心)DOM(文档对象模型)BOM(浏览器对象模型) ES5增加特性1.strict模式  usestrict2.Array增加方法every、forEach、filter、indexOf、lastIndexOf、isArr......
  • map运用
    map可以自动排序,第一个为最小值,返回最大值的时候时间复杂度为log(n)点击查看代码#include<bits/stdc++.h>usingnamespacestd;intmain(){ intn=10; map<int,in......