2

我正在这个网站上寻找线程。我一直在使用代码来回答“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() 的线程执行,但其他线程也执行,即使只调用了四个信号。

为什么这样做?

谢谢,

多纳

4

2 回答 2

6

在我看来,你有Signal错误Wait的方式。如果您希望SaySomething呼叫等待倒计时达到 0,您应该拨打Wait. 这是一个例子:

using System;
using System.Threading;

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();
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("Calling Signal (time #{0})", i);
                CountDown.Signal();
                Thread.Sleep(1000);
            }
            Console.WriteLine("Done"); 
        }

        static void SaySomething(string Something)
        {
            CountDown.Wait();
            Console.WriteLine(Something);
        }

        static void SaySomethingElse(string SomethingElse)
        {
            Thread.Sleep(1000);
            Console.WriteLine(SomethingElse);
        }
    }
}

输出:

Calling Signal (time #0)
Hello From a different Thread
Calling Signal (time #1)
Calling Signal (time #2)
Calling Signal (time #3)
I am Thread one.
I am Thread Three.
I am thread two.
Done
于 2013-05-13T19:59:43.047 回答
1

嗯...在我看来,您想等待所有线程完成,然后再继续主线程。如果是这样,您忘记了 SaySomethingElse() 中的 Signal()。这可以防止 CountDown.CurrentCount 达到 0(零),这就是您的主线程“卡住”的原因。您将其设置为 4,它只会下降到 1(一)。修复它,你应该得到想要的?结果:

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.WriteLine("Done!");
        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);
        CountDown.Signal();
    }
}

输出:

I am Thread one.
I am thread two.
Hello From a different Thread
I am Thread Three.
Done!
于 2013-05-13T20:22:40.610 回答