我正在这个网站上寻找线程。我一直在使用代码来回答“CountdownEvent 是否停止所有线程?”这个问题。我得到的答案是否定的。然后我决定使用传递给 CountdownEvent 的数字。这是我的代码
namespace ThreadPractice
{
class Program
{
static CountdownEvent CountDown = new CountdownEvent(4);
static void Main()
{
new Thread(() => SaySomething("I am Thread one.")).Start();
new Thread(() => SaySomething("I am thread two.")).Start();
new Thread(() => SaySomethingElse("Hello From a different Thread")).Start();
new Thread(() => SaySomething("I am Thread Three.")).Start();
CountDown.Wait();
Console.Read();
}
static void SaySomething(string Something)
{
Thread.Sleep(1000);
Console.WriteLine(Something);
CountDown.Signal();
}
static void SaySomethingElse(string SomethingElse)
{
Thread.Sleep(1000);
Console.WriteLine(SomethingElse);
}
}
}
我期待调用 SaySomethingELse() 的线程执行,但其他线程也执行,即使只调用了四个信号。
为什么这样做?
谢谢,
多纳