C# 使用 AutoResetEvent 或 ManualResetEvent 同步两个线程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; namespace ThreadTest
{
class Program
{
// 信号
static AutoResetEvent ar = new AutoResetEvent(false); // 一个公用的变量
static int i = ; // Thread Method 1
static void WriteMethod(object state) {
while (i < ) {
Thread.Sleep();
i++;
//Console.WriteLine("{1} Write i : {0}", i, Thread.CurrentThread.Name);
Console.WriteLine("{1} Write i : {0}", i, (string)state);
ar.Set();
}
} // Thread Method 2
static void ReadMethod(object state) {
while (true) {
ar.WaitOne();
//Console.WriteLine("{1} Read i : {0}", i, Thread.CurrentThread.Name);
Console.WriteLine("{1} Write i : {0}", i, (string)state);
}
} static void Main(string[] args) {
// 开启 写入线程
// 线程池方法
ThreadPool.QueueUserWorkItem(WriteMethod,"Write Thread "); // 普通开线程方法
//Thread write = new Thread(new ThreadStart(WriteMethod));
//write.Name = "Write Thread "; // 如果是后台线程,需要 一个 Console.ReadKey 等待
//write.IsBackground = true;
//write.Start(); // 开启 读取线程
// 线程池方法
ThreadPool.QueueUserWorkItem(ReadMethod,"Read Thread "); // 普通开线程方法
//Thread read = new Thread(new ThreadStart(ReadMethod));
//read.Name = "Read Thread "; // 如果是后台线程,需要 一个 Console.ReadKey 等待
//read.IsBackground = true;
//read.Start(); // 如果使用线程池方法的时候.线程都是 后台线程. Console 会立即结束.所以需要这个方法来阻塞主线程
Console.ReadKey();
}
}
}
C# 使用 AutoResetEvent 或 ManualResetEvent 同步两个线程的更多相关文章
- C++ 0x 使用condition_variable 与 Mutex 同步两个线程
Mutex : 锁 同一时间只允许一个线程访问其代码内容 拟人 : 就是一把锁而已,可以lock unlock, 谁都可以拿到锁,打开门进屋,但进去后,就会把门锁上(lock) 别人想进就得等他出 ...
- 线程同步(AutoResetEvent与ManualResetEvent)
前言 在我们编写多线程程序时,会遇到这样一个问题:在一个线程处理的过程中,需要等待另一个线程处理的结果才能继续往下执行.比如:有两个线程,一个用来接收Socket数据,另一个用来处理Socket数据, ...
- 基元线程同步构造 AutoResetEvent和ManualResetEvent 线程同步
在.Net多线程编程中,AutoResetEvent和ManualResetEvent这两个类经常用到, 他们的用法很类似,但也有区别.ManualResetEvent和AutoResetEvent都 ...
- AutoResetEvent与ManualResetEvent区别
本文来自:http://www.360doc.com/content/10/1126/10/3267996_72536817.shtml 在.Net多线程编程中,AutoResetEvent和Manu ...
- 个人对AutoResetEvent和ManualResetEvent的理解(转载)
仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...
- C#理解AutoResetEvent和ManualResetEvent
当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考 ...
- C#深入理解AutoResetEvent和ManualResetEvent
当在C#使用多线程时就免不了使用AutoResetEvent和ManualResetEvent类,可以理解这两个类可以通过设置信号来让线程停下来或让线程重新启动,其实与操作系统里的信号量很相似(汗,考 ...
- 个人对AutoResetEvent和ManualResetEvent的理解
一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方法的官方定义并不好理解,什么终止.非 ...
- [转]个人对AutoResetEvent和ManualResetEvent的理解
仅个人见解,不对之处请指正,谢谢. 一.作用 AutoResetEvent和ManualResetEvent可用于控制线程暂停或继续,拥有重要的三个方法:WaitOne.Set和Reset. 这三个方 ...
随机推荐
- book publisher and study
http://www.apress.com/ https://pragprog.com/ https://www.packtpub.com/ http://www.howzhi.com/
- 【原创】StickHeaderListView的简单实现,解决footerView问题
1.前言: 前几天用了GitHub上se.emilsjolander.stickylistheaders这个组件,然后发现这个组件的listview不能添加footerView,加了footer后,滑 ...
- 利用JQ实现的,高仿 彩虹岛官网导航栏(学习HTML过程中的小记录)
利用JQ实现的,高仿 彩虹岛官网导航栏(学习HTML过程中的小记录) 作者:王可利(Star·星星) 总结: 今天学习的jQ类库的使用,代码重复的比较多需要完善.严格区分大小写,在 $(" ...
- EOF 与 getchar()
1.EOF EOF是end of file的缩写,表示"文字流"(stream)的结尾.这里的"文字流",可以是文件(file),也可以是标准输入(stdin) ...
- hdu 5272 Dylans loves numbers
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Dylans loves numbers Description Who is Dylans?Y ...
- Swift :?和 !
Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值, 也就是说变量不会有默认值,所以要求使用变量之前必须要对其初始化 .如果在使用变量之前不进行初始化就会报错: v ...
- 横屏下的ImagePickerController
Try this way.... As per Apple Document, ImagePicker Controller never Rotate in Landscape mode. You h ...
- Android实现传感器应用及位置服务
Android实现传感器应用及位置服务 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 代码实现 这里需用获取加速度传感器和地磁传感器,手机获取旋转的方 ...
- [shell练习]——awk练习题
1. sed和awk有什么区别? (1)awk:按列(域)操作:sed:按行操作(2)awk:文本处理语言,适合对文本进行抽取处理:sed:非交互式的编辑器,适合对文本进行编辑 2. awk要处理域的 ...
- 【BZOJ 2324】 [ZJOI2011]营救皮卡丘
Description 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队一共有N个据点,据点 ...