线程编程-使用SemaphoreSlim类

  

  Semaphore:信号量。 相当于一个计数器,记录一个共享资源被访问线程的个数;好比有两个或者更多个办事窗口,他们都做同一件事;假设有4个窗口,五个窗口都没人访问的时候计数器为4;有一个窗口被占用计数器减1,为3;全部被占用则计数器为0;这时候其他人想要访问就必须等待占用结束后计数器加1;

 

 

  SemaphoreSlim属于混合模式,其允许我们在等待时间很短的情况下无需使用上下文切换。然而,有一个叫做Semaphore类的老版本。该版本使用纯粹的内核时间方式。一般没必要使用它,除非是非常重要的场景。我们可以创建一个具名的Semaphore,就像一个具名的Mutex一样,从而在不同的程序中同步线程。SemaphoreSlim并不使用Windows内核信号量,而且也不支持进程间同步。所以在跨程序同步的场景下可以使用Semaphore

下面的代码展示了,限制同时访问数据库线程为5个。

using System;
using System.Threading;
 
namespace semaphoreSlim_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 6; i++)
            {
                string threadName = "Thread" + i;
                int secondsToWait = 2 + 2 * i;
                var t = new Thread(() => AccessDatabase(threadName, secondsToWait));
                t.Start();
            }
 
            Console.ReadKey();
        }
 
        static SemaphoreSlim _semaphor = new SemaphoreSlim(4);
 
        static void AccessDatabase(string name,int seconds)
        {
            Console.WriteLine("{0} waits to access a database", name);
            _semaphor.Wait();
            Console.WriteLine("{0} was granted an access to a database", name);
            Thread.Sleep(TimeSpan.FromSeconds(seconds));
            Console.WriteLine("{0} is completed", name);
            _semaphor.Release();
        }
    }
}

  当主程序启动时,创建了一个SemaphoreSlim的一个实例,并在其构造函数中指定允许的并发线程的数量,然后启动6个不同名称和不同初始运行时间的线程。

  每个线程都尝试获取数据库的访问,但我们借助于信号系统限制了访问数据库的并发线程最多为4个。所以当有4个线程获取了数据库的访问时,其余线程需等待,直到之前线程中的某一个完成工作并调用_semaphor.Release()方法来发出信号。

猜你喜欢

转载自www.cnblogs.com/gougou1981/p/12342203.html