这是Cuts在Nutshell中提供的一个示例程序的略微修改版本:
using System;
using System.Threading;
namespace SemaphorTest
{
class Program
{
static Semaphore gate = new Semaphore(3, 3);
static void Main(string[] args)
{
for (int i = 0; i < 5; i++)
{
Thread t = new Thread(Enter);
t.Start(i);
}
}
public static void Enter(Object id)
{
Console.WriteLine("Thread " + id + " wants to enter.");
gate.WaitOne();
Console.WriteLine("Thread " + id + " is in.");
Thread.Sleep(500 * (Int32)id);
Console.WriteLine("Thread " + id + " leaving.");
gate.Release();
}
}
}
这将打印以下输出(看似以随机顺序):
Thread 0 wants to enter.
Thread 1 wants to enter.
Thread 1 is in.
Thread 0 is in.
Thread 2 wants to enter.
Thread 3 wants to enter.
Thread 0 leaving.
Thread 3 is in.
Thread 4 wants to enter.
Thread 2 is in.
Thread 1 leaving.
Thread 4 is in.
Thread 2 leaving.
Thread 3 leaving.
Thread 4 leaving.
但是,如下添加Thread.Join()会极大地改变输出。我:
for (int i = 0; i < 5; i++)
{
Thread t = new Thread(Enter);
t.Start(i);
t.Join();
}
将输出更改为:
Thread 0 wants to enter.
Thread 0 is in.
Thread 0 leaving.
Thread 1 wants to enter.
Thread 1 is in.
Thread 1 leaving.
Thread 2 wants to enter.
Thread 2 is in.
Thread 2 leaving.
Thread 3 wants to enter.
Thread 3 is in.
Thread 3 leaving.
Thread 4 wants to enter.
Thread 4 is in.
Thread 4 leaving.
为什么会这样?我知道这些线程默认是前台线程,并且主线程不需要等待它们完成(它们将在Main完成后运行,因此它们不需要thread.Join())。但我不明白是什么让它们按顺序运行,因为它们是在更改后创建的。有什么想法吗?
Thanks
Dileep Balakrishnan
答案 0 :(得分:8)
为什么会这样?
因为你问过它!
你已经启动了一个线程,然后等待它完成,然后启动下一个线程等。这正是Thread.Join
所做的:它阻塞当前正在执行的线程,直到你调用它的线程终止
我感兴趣的是你期望要执行的代码...如果你只是想等到所有线程都完成之后才让主线程完成,你需要启动所有线程,在你去的时候记住它们,然后依次在每个线程上调用Join
。例如:
List<Thread> threads = new List<Thread>();
// First start all the threads
for (int i = 0; i < 5; i++)
{
Thread t = new Thread(Enter);
t.Start(i);
threads.Add(t);
}
// Then wait for them to finish
foreach (var thread in threads)
{
thread.Join();
}
答案 1 :(得分:1)
答案 2 :(得分:0)
Thread.join()不会使事情同时运行。它实际上通过阻止当前线程直到它完成来强制执行您所看到的行为。 文档中明确提到了这一点:http://msdn.microsoft.com/en-us/library/95hbf2ta.aspx