lock模拟CountDownEvent
Posted 听哥哥的话
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lock模拟CountDownEvent相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; namespace ConsoleApplication44 { class Class2 { public readonly static object _locker = new object(); public static bool _singal; public static int _count; public static void Main() { _count = 3; for (int i = 1; i <= 3; i++) { new Thread(DoWork).Start(i); } Wait(); Console.WriteLine("Compelete!"); Console.ReadKey(); } public static void DoWork(object i) { Set(); Console.WriteLine(i); } public static void Set() { lock (_locker) { _count--; Monitor.PulseAll(_locker); } } public static void Wait() { lock (_locker) { while (_count > 0) { Monitor.Wait(_locker); } } } } }
以上是关于lock模拟CountDownEvent的主要内容,如果未能解决你的问题,请参考以下文章