/// <summary> /// 信号量,类似于占坑机制,初始设为5个空的坑位,且最大5个位置 /// </summary> static readonly Semaphore semaphore = new Semaphore(5, 5); static void Test() { Task.Run(AAA); BBB(); } static void AAA() { while (true) { semaphore.WaitOne(); //没有信号量就会阻塞在这里,否则会消耗一个信号量 Console.WriteLine("占个坑"); } } static void BBB() { while (true) { Thread.Sleep(3000); Console.WriteLine("释放一个坑"); semaphore.Release(); //释放一个信号量 } }
标签:AAA,C#,void,Semaphore,信号量,static,semaphore From: https://www.cnblogs.com/luludongxu/p/17688144.html