C# 线程同步

时间:2021-08-18 15:10:21   收藏:0   阅读:16

测试线程同步机制 AutoResetEvent:

 1         static AutoResetEvent wakeup = new AutoResetEvent(false);
 2         static void pthread_func()
 3         {
 4             while (true)
 5             {
 6 
 7                 Console.WriteLine("等待唤醒");
 8                 wakeup.WaitOne();
 9                 Console.WriteLine("线程唤醒");
10                 Thread.Sleep(1000);
11             }
12         }
13         static void Main(string[] args)
14         {
15             Thread tmp = new Thread(pthread_func);
16             tmp.Start();
17             Thread.Sleep(1000);
18             Console.WriteLine("发送唤醒");
19             wakeup.Set();
20             Thread.Sleep(2000);
21             Console.WriteLine("发送唤醒");
22             Thread.Sleep(2000);
23             wakeup.Set();
24         }

终端显示:

技术分享图片

 

原文:https://www.cnblogs.com/chenxiaolinembed/p/15155959.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!