ManulResetEvent与AutoResetEvent

定义

ManualResetEvent:通知正在等待的线程已发生事件。

AutoResetEvent:通知正在等待的线程已发生事件。

代码

ManualResetEvent测试

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

namespace NetThreading.UserThread

{

/// <summary>

/// ManualAutoResetEvent的用法

/// </summary>

public class ManualResetEventApp:IMain

{

#region IMain 成员

public void MainRun()

{

Console.WriteLine("ManualResetEvent ---------------------------------");

//指定ManualResetEvent的初始状态为无信号状态,ManualResetEvent等待5秒后超时,

//ManualResetEvent的状态仍是无信号状态

Console.WriteLine("ManualResetEvent default state:fase ");

ManualResetEvent manualResetEvent = new ManualResetEvent(false);

//ManualResetEvent 初始值为false,ManualResetEvent一直等待有信号,等待超时时间为5s

Console.WriteLine("ManualResetEvent 初始值为false,ManualResetEvent一直等待有信号,等待超时时间为5s");

bool currentResult=manualResetEvent.WaitOne(TimeSpan.FromSeconds(5));

Console.WriteLine(string.Format("ManualResetEvent:After wait State:{0}",currentResult));

//指定ManualResetEvent的初始状态为有信号状态,ManualResetEvent等待5秒后超时,

//ManualResetEvent的状态仍是有信号状态

Console.WriteLine("ManualResetEvent default state:true ");

manualResetEvent = new ManualResetEvent(true );

//ManualResetEvent 初始值为true,ManualResetEvent不用等待,超时时间不起作用

Console.WriteLine("ManualResetEvent 初始值为true,ManualResetEvent不用等待,超时时间不起作用");

currentResult = manualResetEvent.WaitOne(TimeSpan.FromSeconds(5));

Console.WriteLine(string.Format("ManualResetEvent:After wait State:{0}", currentResult));

//改变信号状态

Console.WriteLine("改变信号状态");

Console.WriteLine("ManualResetEvent default state:fale ");

manualResetEvent = new ManualResetEvent(false );

Console.WriteLine("will set");

manualResetEvent.Set();

Console.WriteLine("ManualResetEvent 设置为有信号,状态改变,ManualResetEvent不会等待");

Console.WriteLine("after set ");

currentResult = manualResetEvent.WaitOne(TimeSpan.FromSeconds(10));

Console.WriteLine(string.Format("ManualResetEvent:After wait State:{0}", currentResult));

Console.WriteLine("AutoResetEvent 再次等待(超时时间10s)");

currentResult = manualResetEvent.WaitOne(TimeSpan.FromSeconds(10));

Console.WriteLine(string.Format("manualResetEvent:After wait State:{0}", currentResult));

}

#endregion

}

}

AutoResetEvent测试

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

namespace NetThreading.UserThread

{

/// <summary>

/// AutoResetEventApp的用法

/// </summary>

public class AutoResetEventApp:IMain

{

#region IMain 成员

public void MainRun()

{

Console.WriteLine("AutoResetEvent ---------------------------------");

//指定AutoResetEvent的初始状态为无信号状态,AutoResetEvent等待5秒后超时,

//AutoResetEvent的状态仍是无信号状态

Console.WriteLine("AutoResetEvent default state:fase ");

AutoResetEvent autoResetEvent = new AutoResetEvent(false);

//AutoResetEvent 初始值为false,AutoResetEvent一直等待有信号,等待超时时间为5s

Console.WriteLine("AutoResetEvent 初始值为false,AutoResetEvent一直等待有信号,等待超时时间为5s");

bool currentResult = autoResetEvent.WaitOne(TimeSpan.FromSeconds(5));

Console.WriteLine(string.Format("AutoResetEvent:After wait State:{0}", currentResult));

//指定AutoResetEvent的初始状态为有信号状态,AutoResetEvent等待5秒后超时,

//AutoResetEvent的状态仍是有信号状态

Console.WriteLine("AutoResetEvent default state:true ");

autoResetEvent = new AutoResetEvent(true);

//AutoResetEvent 初始值为true,AutoResetEvent不用等待,超时时间不起作用

Console.WriteLine("AutoResetEvent 初始值为true,AutoResetEvent不用等待,超时时间不起作用");

currentResult = autoResetEvent.WaitOne(TimeSpan.FromSeconds(5));

Console.WriteLine(string.Format("AutoResetEvent:After wait State:{0}", currentResult));

//改变信号状态

Console.WriteLine("改变信号状态");

Console.WriteLine("AutoResetEvent default state:fale ");

autoResetEvent = new AutoResetEvent(false);

Console.WriteLine("will set");

autoResetEvent.Set();

Console.WriteLine("AutoResetEvent 设置为有信号,状态改变,AutoResetEvent不会等待");

Console.WriteLine("after set ");

currentResult = autoResetEvent.WaitOne(TimeSpan.FromSeconds(10));

Console.WriteLine(string.Format("AutoResetEvent:After wait State:{0}", currentResult));

Console.WriteLine("AutoResetEvent 再次等待(超时时间10s)");

currentResult = autoResetEvent.WaitOne(TimeSpan.FromSeconds(10));

Console.WriteLine(string.Format("AutoResetEvent:After wait State:{0}", currentResult));

}

#endregion

}

}

运行结果

clip_image002

结论

共同点:

ManualResetEvent与AutoResetEvent都是在没有信号的情况下等待。一旦有信号,两者都是立即执行,不等待。

不同点:

ManualResetEvent:信号发生改变之后在下一次等待中信号状态不会发生变化,而AutoResetEvent在信号发生之后再次等待,它的信号状态又被自动重置为无信号。

转载于:https://www.cnblogs.com/hbb0b0/archive/2011/01/13/1934430.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/435256.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Timer

[摘要] Timer是实时操作系统的一个重要组成部分。本文结合近阶段的学习和实验情况&#xff0c;对VxWorks中的时间函数和定时器作了一些探讨。主要介绍了Timer的机制&#xff0c;相关的函数&#xff0c;并给出了一些具体的例子。 一. Tick Tick是指每秒中定时器中断的次数。POS…

神经网络与深度学习——TensorFlow2.0实战(笔记)(三)(python常量、变量和表达式)

从程序中学习知识点 1. #支持6中表达形式 数字 字符串 列表 元组 字典 集合 #数字 整型(正整数 负整数 零) #Python3中的整数可以任意大&#xff0c;而不用担心位数不够而导致溢出的情况 intnum12345678909999999999999 print(intnum,type(intnum)) #浮点数 小数 floatnum1 …

reentrantlock非公平锁不会随机挂起线程?_程序员必须要知道的ReentrantLock 及 AQS 实现原理...

专注于Java领域优质技术&#xff0c;欢迎关注作者&#xff1a;Float_Luuu提到 JAVA 加锁&#xff0c;我们通常会想到 synchronized 关键字或者是 Java Concurrent Util(后面简称JCU)包下面的 Lock&#xff0c;今天就来扒一扒 Lock 是如何实现的&#xff0c;比如我们可以先提出一…

关于SOAP的几篇文章

转载自&#xff1a;/show-1598-1.shtml PHP操作soap我总觉得是一件非常痛苦的事情&#xff0c;但没有办法&#xff0c;现在很多功能都是基于WebService的&#xff0c;比如那个amazon的&#xff0c;但其实很多 公司都也还是提供了restful之类的接口&#xff0c;使得PHP与其他系统…

VxWorks动态加载

注&#xff1a;最近在做热补丁的功能&#xff0c;看到一篇gateway写的文章&#xff0c;觉得很通俗易懂的&#xff0c;就将搜集到的资料又整理了一下&#xff0c;供大家参考。 使用动态加载目标模块的方式有很多好处&#xff0c;比如可以在不破坏原来的环境下增加调试定位功能&a…

excel办公常用的宏_让领导看傻!精美168套办公常用excel模板免费领

HR们面试的时候&#xff0c;是不是经常看到应聘者的简历上技能那一栏写着精通Excel、PPT等办公技能&#xff1f;你知道Excel用到什么程度才算精通吗&#xff1f;能够用excel做表格就算精通吗&#xff1f;还是要能够熟练使用各种函数&#xff1f;你做出来的Excel报表也许是这样的…

神经网络与深度学习——TensorFlow2.0实战(笔记)(三)(python运算符和表达式)

从程序中学习知识点 1.算术运算符 #运算符&#xff08;Operator&#xff09;&#xff1a;完成不同类型的常量、变量之间的运算 #除法运算 / 结果是一个浮点型的精确数的值&#xff0c;与java等其他语言的不同之处 print(7/2,7.0/2,-7/2) #整除运算 print(7//2,-7//2) print(7…

VxWorks系统BSP配置文件及生成下载

%A VxWorks BSP主要配置文件 config.h , Makefile 注解和 BSP生成下载实例。%A%A 相关内容可参考 VxWorks BSP和启动过程%A%A config.h文件配置%A%A /*%A This file contains the configuration parameters for the CPU evaluation board.%A */%A%A #ifndef INCconfigh%A #defi…

float32精度_PyTorch 1.6来了:新增自动混合精度训练、Windows版开发维护权移交微软...

刚刚&#xff0c;Facebook 通过 PyTorch 官方博客宣布&#xff1a;PyTorch 1.6 正式发布&#xff01;新版本增加了一个 amp 子模块&#xff0c;支持本地自动混合精度训练。Facebook 还表示&#xff0c;微软已扩大了对 PyTorch 社区的参与&#xff0c;现在拥有 PyTorch 在 Windo…

神经网络与深度学习——TensorFlow2.0实战(笔记)(三)(python语句)

1.if语句 #if语句 x,y 3,5 if x<y:print("x<y") elif xy:print("xy") else:print("x>y") 2.条件表达式 x,y3,5 #表达式1(条件为真的结果) if 判断条件 else 表达式2(条件为假的结果) print(x if x>y else y) 3.while语句 #死循环…

建立调试环境

建立调试环境 Tornado采用支持主机/目标机开发模式。本节以x86系列目标机为例介绍调试环境的建立 。 7.1.1 配置文件config.h 目标机运行的程序包括两部分&#xff1a;引导文件bootrom.sys和操作系统影像文件VxWorks。 引导文件bootrom.sys的主要作用类似于BIOS&#xf…

天气预报的获取

好久没有写技术文章了&#xff0c;2010年工作太忙&#xff0c;奔波在国内各地&#xff0c;也没什么时间关注一些技术方面的事情&#xff0c;最近有一个项目封闭开发&#xff0c;有些时间来写些琐碎的东西&#xff0c;就当是整理下最近的东西吧&#xff0c;没什么技术价值&#…

神经网络与深度学习——TensorFlow2.0实战(笔记)(四)(python列表与元组)

序列数据结构 1.成员是有序排列的 2.每个元素的位置称为下标或索引 3.通过索引访问序列中的成员 4.Python中的序列数据类型有字符串、列表、元组 "abc" ≠ "bca" 5.Python中的列表和元组&#xff0c;可以存放不同类型的数据 列表使用方括号[ ]表示&a…

apktoolkit apk反编译没有文件_[工具] Mac下一键APK逆向环境

安装apktool和dex2jar,jd-guihomebrew安装&#xff1a; brew install apktool brew install dex2jar JD-GUI去http://jd.benow.ca/下载 dmg可能不支持最新版本的mac用不了&#xff0c;打开就报错反编译流程执行脚本apktool d xxx.apk 注&#xff1a;xxx.apk为你要反编译的apk…

搭建你的嵌入式Vxworks开发环境

3.1 最常见的开发环境配置使用串口和网络连接&#xff08;host和target之间&#xff09;。串口连接用于和boot loader之间的通信&#xff08;如输出信息在host上的显示&#xff09;&#xff0c;网络连接用于传输文件&#xff0c;包括Vxworks system image。默认情况下使用网络连…

python开发应用程序错误_Python 程序员经常犯的 10 个错误

常见错误 #6: 不明白Python在闭包中是如何绑定变量的 看下面这个例子&#xff1a; >>> def create_multipliers(): ... return [lambda x : i * x for i in range(5)] >>> for multiplier in create_multipliers(): ... print multiplier(2) ... 你也许希望获…

神经网络与深度学习——TensorFlow2.0实战(笔记)(四)(python字典和集合)

字典和集合 字典 每个字典元素都是一个键(关键字)/值(关键字对应的取值)对 #创建字典 dic_score{"语文":80,"数学":99} #打印 print(dic_score) print(dic_score["语文"]) #长度 print(dic_score.__len__)#错误写法 print(len(dic_score)) #…

VxWorks动态加载.out文件

//Device.cpp #include "other.h" #ifdef __cplusplus extern "C" { #endif int initDevice(char *arg); #ifdef __cplusplus } #endif int initDevice(char *arg) { printf("%s\n", arg); } 生成的.out文件需对其使用如下命令 chmod.exe arx…

[Project Euler]加入欧拉 Problem 9

A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 b2 c2 For example, 32 42 9 16 25 52. There exists exactly one Pythagorean triplet for which a b c 1000.Find the product abc. 意思很明白找出这样的a, b, c abc 1000 a …

java 在已有的so基础上封装jni_[干货]再见,Android JNI 封装

1 前言2 JNI 速查表2.1 Java 和 Native 数据类型映射表2.2 引用类型3 JNI 理论基础速览4 JNI 常用场景示例4.1 字符串传递(java->native)4.2 字符串返回(native->java)4.3 数组传递(java->native)4.4 其他复杂对象传递(java->native)4.5 复杂对象返回(native->j…