要了解如何启动调用相同方法的多个线程,我想使用以下逻辑:
如果我使用Join函数或直接启动线程,例程可以正常工作:
new Thread(EnterSemaphore).Start(i);
我从结果中看到的是我正在使用的例程不起作用。并非所有线程都显示,我甚至看到索引号为6的线程。我想了解为什么启动这样的线程数组会失败。
class SemaphoreLock
{
#region Fields
// SemaphoreSlim _sem heeft de capaciteit voor 3 elementen
static SemaphoreSlim _sem = new SemaphoreSlim(3); // Capacity of 3
public readonly object StartLocker = new object();
#endregion
#region Constructor
//
public SemaphoreLock()
{
Thread[] testStart = new Thread[10];
for (int i = 1; i <= 5; i++)
{
// If enabled, this works
// new Thread(EnterSemaphore).Start(i);
// This is not working.
testStart[i] = new Thread(() => EnterSemaphore(i));
lock (StartLocker) testStart[i].Start();
// Adding a join here works, every thread is started as a single thread
//testStart[i].Join();
}
}
#endregion
#region Methods
static void EnterSemaphore(object id)
{
Console.WriteLine(id + " wants to enter");
// Block the current thread until it can enter the Semaphore
_sem.Wait();
Console.WriteLine(id + " is in!");
Thread.Sleep(1000 * (int)id);
Console.WriteLine(id + " is leaving");
_sem.Release();
if (_sem.CurrentCount == 3)
{
Console.WriteLine("Done...");
}
}
#endregion
}
答案 0 :(得分:1)
您的代码运行正常。通过使用线程Join
,您可以看到不同的线程,因为Thread.Join
:
阻止调用线程,直到由此表示的线程 实例终止,同时继续执行标准COM和 SendMessage抽水。
作为另一个示例,您还可以使用Thread.Sleep()
来查看类似的行为:
testStart[i] = new Thread(() => EnterSemaphore(i));
lock (StartLocker) testStart[i].Start();
Thread.Sleep(100);
更好的是,为了见证这种行为,在for循环结束时使用ReadLine()
:
for (int i = 1; i <= 5; i++)
{
testStart[i] = new Thread(() => EnterSemaphore(i));
lock (StartLocker) testStart[i].Start();
}
Console.ReadLine();