C# ManualResetEvent的使用
2024-08-25 00:33:40
using System;
using System.Threading; public class Example
{
// mre is used to block and release threads manually. It is
// created in the unsignaled state.
private static ManualResetEvent mre = new ManualResetEvent(false); static void Main()
{
Console.WriteLine("\nStart 3 named threads that block on a ManualResetEvent:\n"); for(int i = ; i <= ; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
} Thread.Sleep();
Console.WriteLine("\nWhen all three threads have started, press Enter to call Set()" +
"\nto release all the threads.\n");
Console.ReadLine(); mre.Set(); Thread.Sleep();
Console.WriteLine("\nWhen a ManualResetEvent is signaled, threads that call WaitOne()" +
"\ndo not block. Press Enter to show this.\n");
Console.ReadLine(); for(int i = ; i <= ; i++)
{
Thread t = new Thread(ThreadProc);
t.Name = "Thread_" + i;
t.Start();
} Thread.Sleep();
Console.WriteLine("\nPress Enter to call Reset(), so that threads once again block" +
"\nwhen they call WaitOne().\n");
Console.ReadLine(); mre.Reset(); // Start a thread that waits on the ManualResetEvent.
Thread t5 = new Thread(ThreadProc);
t5.Name = "Thread_5";
t5.Start(); Thread.Sleep();
Console.WriteLine("\nPress Enter to call Set() and conclude the demo.");
Console.ReadLine(); mre.Set(); // If you run this example in Visual Studio, uncomment the following line:
//Console.ReadLine();
} private static void ThreadProc()
{
string name = Thread.CurrentThread.Name; Console.WriteLine(name + " starts and calls mre.WaitOne()"); mre.WaitOne(); Console.WriteLine(name + " ends.");
}
} /* This example produces output similar to the following: Start 3 named threads that block on a ManualResetEvent: Thread_0 starts and calls mre.WaitOne()
Thread_1 starts and calls mre.WaitOne()
Thread_2 starts and calls mre.WaitOne() When all three threads have started, press Enter to call Set()
to release all the threads. Thread_2 ends.
Thread_0 ends.
Thread_1 ends. When a ManualResetEvent is signaled, threads that call WaitOne()
do not block. Press Enter to show this. Thread_3 starts and calls mre.WaitOne()
Thread_3 ends.
Thread_4 starts and calls mre.WaitOne()
Thread_4 ends. Press Enter to call Reset(), so that threads once again block
when they call WaitOne(). Thread_5 starts and calls mre.WaitOne() Press Enter to call Set() and conclude the demo. Thread_5 ends.
*/
最新文章
- Android--短信
- PHP IIS SPY
- 运用HBuilder上传到GitHub
- 织梦dede标签tags的美化教程
- 淘特房产CMS系统 7.5
- document.documentElement 和document.body 以及其属性
- 关于knob.js进度插件的使用
- 我的TDD实践---UnitTest单元测试
- iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。
- java中的final与static
- hibernate解读之session--基于最新稳定版5.2.12
- RobotFramework下的http接口自动化Get Response header 关键字的使用
- 牛客小白月赛13-J小A的数学题 (莫比乌斯反演)
- EffectiveC++ 第2章 构造/析构/赋值运算
- 干了这杯Java之HashMap
- 三十四、Linux 进程与信号——信号特点、信号集和信号屏蔽函数
- PHP实现网络Socket及IO多路复用
- 2017-2018-2 20165314 实验三《 敏捷开发与XP实践》实验报告
- [Android] websocket客户端开发
- [Big Data - Kafka] kafka学习笔记:知识点整理
热门文章
- 从0开始学Java——从jsp到servlet转换的各种辅助元素介绍
- JAVA中获取当前系统时间
- [CareerCup] 9.11 Parenthesize the Expression 表达式加括号
- Linux10.11-10.18)学习笔记(
- 掌握GCD以及后台永久运行的代码 (使用GCD处理后台线程和UI线程的交互)
- IOS开发之——意见反馈UITextView的使用+不能输入字符输入
- 20145215《Java程序设计》第3周学习总结
- java <;? super Fruit>;与<;? extends Fruit>;
- Mustache.js前端模板引擎源码解读
- jQuery UI dialog