目前项目里面有个需求,需要多线程操作数据库,等待数据库操作完毕之后,主线程才能继续执行以后的操作。
想了想,最后决定使用两个信号量来完成这个需求。
具体需求如下 :
- 操作数据库的线程最多20个
- 主线程必须等待所有操作数据库的线程返回后,才能进行下一步操作
- 声明一个最大值为20,初始值为20的信号量s1和一个最大值为1,初始值为0的信号量s2,以及一个任务计数器count。
- 将需要线程池操作的任务数赋给count。
- 在调用线程池的循环中,调用s1的WaitOne方法,在线程的具体操作执行完毕后调用s1的Release方法,并且使用Interlocked的Decrement方法将任务数count减1,并且判断count是否等于0,如果count等于0,调用s2的Release方法。
- 最后在调用线程池的循环结束之后,调用s2的WaitOne方法。
运行截图如下:
具体代码如下:
using System;
using System.Threading;
using System.Collections.Generic;
public class UseSemaphore
{
private static readonly Semaphore s1 = new Semaphore(3,3);
private static readonly Semaphore s2 = new Semaphore(0,1);
private const int TaskCount = 10;
private static int count;
public static void Main()
{
Console.WriteLine("MainThread Start");
count = TaskCount;
for(int i = 0; i < TaskCount; ++i)
{
s1.WaitOne();
ThreadPool.QueueUserWorkItem(Process, null);
}
s2.WaitOne();
Console.WriteLine("MainThread");
Console.ReadKey();
}
private static void Process(object obj)
{
Console.WriteLine("ThreadPool {0} Start", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(100);
Console.WriteLine("ThreadPool {0} Stop", Thread.CurrentThread.ManagedThreadId);
s1.Release();
Interlocked.Decrement(ref count);
if(count == 0)
{
Console.WriteLine("s2 Release");
s2.Release();
}
}
}
using System.Collections.Generic;
public class UseSemaphore
{
private static readonly Semaphore s1 = new Semaphore(3,3);
private static readonly Semaphore s2 = new Semaphore(0,1);
private const int TaskCount = 10;
private static int count;
public static void Main()
{
Console.WriteLine("MainThread Start");
count = TaskCount;
for(int i = 0; i < TaskCount; ++i)
{
s1.WaitOne();
ThreadPool.QueueUserWorkItem(Process, null);
}
s2.WaitOne();
Console.WriteLine("MainThread");
Console.ReadKey();
}
private static void Process(object obj)
{
Console.WriteLine("ThreadPool {0} Start", Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(100);
Console.WriteLine("ThreadPool {0} Stop", Thread.CurrentThread.ManagedThreadId);
s1.Release();
Interlocked.Decrement(ref count);
if(count == 0)
{
Console.WriteLine("s2 Release");
s2.Release();
}
}
}