C#多线程开发10:线程同步之Semaphore类

C#多线程开发10:线程同步之Semaphore类

分类:C#

Semaphore类表示信号量。

信号量和互斥类似,,只是信号量可以同时由多个线程使用,而互斥只能由一个线程使用。也就是说,使用信号量时,可以多个线程同时访问受保护的资源。下面实例演示了“学生到食堂就餐”的场景,一共有10个学生需要就餐,但是食堂每次只能接纳4名学生就餐,所以将信号量的计数设置为4,每次有4个任务(就餐任务)可以获得锁定。剩下的学生就必须等待,等到锁定被解除时,学生才可以继续获得锁定,进入食堂就餐。

using System;using System.Threading;using System.Threading.Tasks;namespace SemaphoreExample{class Program{static void Main(string[] args){int studentCount = 10;int seatCount = 4;//小小食堂只有4个位子var semaphore = new SemaphoreSlim(seatCount, seatCount);var eatings = new Task[studentCount];for (int i = 0; i < studentCount; i++){eatings[i] = Task.Run(() => Eat(semaphore));}Task.WaitAll(eatings);Console.WriteLine("All students have finished eating!");}static void Eat(SemaphoreSlim semaphore){semaphore.Wait();try{Console.WriteLine("Student {0} is eating now!", Task.CurrentId);Thread.Sleep(1000);}finally{Console.WriteLine("Student {0} have finished eating!", Task.CurrentId);semaphore.Release();}}}}

代码的执行结果如下图所示。

使用Semaphore类的实例方法Wait()锁定信号量,使用Release()方法可以解除锁定。

.Net中信号量有两个类:Semaphore和SemaphoreSlim。Semaphore类可以命名,允许在不同进程之间同步。SemaphoreSlim类是对于较短等待时间进行了优化的轻型版本。本文中使用的是SemaphoreSlim类。

版权声明:本文为博主原创文章,未经博主允许不得转载。

上一篇C#多线程开发9:使用Mutex类禁止应用程序启动两次下一篇使用Linq的Intersect与Except方法的实例

顶0踩0

最有效的资本是我们的信誉,它24小时不停为我们工作。

C#多线程开发10:线程同步之Semaphore类

相关文章:

你感兴趣的文章:

标签云: