我需要使用信号量并行完成一些任务。我试试这个:
Semaphore sema = new Semaphore(2,2);
Thread[] Threads = new Thread[5];
for (int k = 0; k < 5; k++) {
sema.WaitOne();
Console.WriteLine((k + 1) + " started");
Threads[k] = new Thread(ThreadMethod1);
Threads[k].Start(k + 1);
sema.Release();
}
static void ThreadMethod1(object id) {
Thread.Sleep(50);
Console.WriteLine(id + " completed");
}
输出如下:
1 started
2 started
3 started
4 started
5 started
1 completed
2 completed
4 completed
3 completed
5 completed
Isn的信号量应该只允许2个线程运行吗?我不明白或做错了什么?
答案 0 :(得分:8)
您正在&#34; main&#34;中输入/退出信号量。线。它没用,因为在每个&#34;周期&#34;你进入和退出它。在这个修改过的例子中,你在主线程中输入信号量,在完成工作线程后退出它。
请注意,我必须将信号量传递给工作线程(我使用了Tuple
,但其他方法都可以)
static void Main(string[] args) {
Semaphore sema = new Semaphore(2, 2);
Thread[] Threads = new Thread[5];
for (int k = 0; k < 5; k++) {
sema.WaitOne();
Console.WriteLine((k + 1) + " started");
Threads[k] = new Thread(ThreadMethod1);
Threads[k].Start(Tuple.Create(k + 1, sema));
}
}
static void ThreadMethod1(object tuple) {
Tuple<int, Semaphore> tuple2 = (Tuple<int, Semaphore>)tuple;
Thread.Sleep(50);
Console.WriteLine(tuple2.Item1 + " completed");
tuple2.Item2.Release();
}
您可以移动sema.WaitOne
&#34;内部&#34; ThreadMethod1
,但它会有所不同:所有线程都会被创建但是会等待#34;并且每次只有2个人会做真正的工作&#34;。如编写而不是创建最多两个线程(并完成工作)
答案 1 :(得分:0)
您所要做的就是从主线程移动信号量上的操作。对代码的小修正将解决它。
public static Semaphore sema = new Semaphore(2, 2);
static void Main(string[] args)
{
Thread[] Threads = new Thread[5];
for (int k = 0; k < 5; k++)
{
Console.WriteLine((k + 1) + " started");
Threads[k] = new Thread(ThreadMethod1);
Threads[k].Start(k + 1);
}
}
static void ThreadMethod1(object id)
{
sema.WaitOne();
Thread.Sleep(1000);
Console.WriteLine(id + " completed");
sema.Release();
}