线程通知机制 关键字 AutoResetEvent
来源:互联网 发布:访客网络加密 编辑:程序博客网 时间:2024/05/22 04:46
线程通知
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace ThreadTest{ class Program { ///用static来构造AutoresetEvetn就没有必要再构造函数构造,而且在main函数之前 /// 就已经构造好了 private static AutoResetEvent myResetEvent = new AutoResetEvent(false); private static int number; static void MyreadThreadProc() { while (true) { ///等待接受信号然后读出 myResetEvent.WaitOne(); Console.WriteLine("{0}读到的值是:{1}", Thread.CurrentThread.Name, number); } } static void Main(string[] args) { Thread myReadThread = new Thread(new ThreadStart(MyreadThreadProc)); myReadThread.Name = "读进程"; myReadThread.Start(); for(int i = 0; i <100;i++) { Console.WriteLine("写进程写的是{0}", i); number = i; myResetEvent.Set(); Thread.Sleep(1); } } }}
</pre><pre code_snippet_id="1863795" snippet_file_name="blog_20160902_3_8848226" name="code" class="csharp">
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace ThreadTest{ /// <summary> /// 一个很简单的例子来演示waitany和waitall /// 注意都是静态方法 所以用Autoresetevent... /// </summary> class Person { private AutoResetEvent[] autoResetEvent; public Person() { autoResetEvent = new AutoResetEvent[] { new AutoResetEvent(false), new AutoResetEvent(false), new AutoResetEvent(false), }; } /// 定义线程处理事件 public void GetCar() { Console.WriteLine("我捡到宝马了"); autoResetEvent[0].Set(); } public void GetMoney() { Console.WriteLine("我赚到钱啦"); autoResetEvent[1].Set(); } public void GetWife() { Console.WriteLine("我骗到老婆啦"); autoResetEvent[2].Set(); } static void Main(string[] args) { ///设置三个线程 Person p = new Person(); Thread threadA = new Thread(new ThreadStart(p.GetCar)); threadA.Start(); Thread threadB = new Thread(new ThreadStart(p.GetMoney)); threadB.Start(); Thread threadC = new Thread(new ThreadStart(p.GetWife)); threadC.Start(); AutoResetEvent.WaitAll(p.autoResetEvent); Console.WriteLine("生活如此美好"); } }}
1 0
- 线程通知机制 关键字 AutoResetEvent
- AutoResetEvent 一个体现通知机制的例子
- java线程同步的等待通知机制
- java线程等待/通知机制及中断
- 线程同步 - EventWaitHandle & AutoResetEvent & ManualResetEvent
- ManualResetEvent,AutoResetEvent类线程同步
- AutoResetEvent和ManualResetEvent 线程同步
- 线程同步:ManualResetEvent和AutoResetEvent
- java线程第四课:线程的等待通知机制
- AutoResetEvent
- AutoResetEvent
- AutoResetEvent
- AutoResetEvent
- AutoResetEvent
- AutoResetEvent
- AutoResetEvent
- AutoResetEvent
- c# 线程池中使用AutoResetEvent(事件)
- 哈希表创建和处理冲突的方法
- 数字的任何位数的提取
- C#的第8课内容
- 开启我的博客之旅
- Maven 聚合和继承(5)
- 线程通知机制 关键字 AutoResetEvent
- *BaseAdapter
- Android之ndk之gdb调试
- [ASP.NET MVC 大牛之路]01 - 开篇
- 深度优先搜索dfs(poj 1655)
- poj 3393 Lucky and Good Months by Gregorian Calendar
- Android 5.0 之TabLayout
- Maven 和Eclipse的整合(6)
- 工作感想-软件测试工程师