Thread.Join()改变c#中的程序输出

本文关键字:程序 输出 改变 Join Thread | 更新日期: 2023-09-27 18:07:59

这是c#简览中给出的一个示例程序的稍微修改版本:

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.

为什么会发生这种情况?我知道这些线程在默认情况下是前台线程,主线程不需要等待它们完成(它们甚至会在主线程完成后运行,所以它们不需要在这里使用thread. join())。但我不明白是什么使它们按照更改后创建的顺序运行。有什么想法吗?

Thanks
Dileep Balakrishnan 

Thread.Join()改变c#中的程序输出

为什么会发生这种情况?

因为是你让它这么做的!

你已经启动了一个线程,然后等待它完成,然后启动下一个线程,等等。这正是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();
}

正在将每个线程与调用线程连接起来。这实际上会阻塞调用线程,并等待每个线程依次结束。

在文档中声明:

阻塞调用线程,直到线程终止。

Thread.join()不能并发运行。它实际上通过阻塞当前线程来强制执行你所看到的行为,直到它完成。这在文档中有明确的说明:http://msdn.microsoft.com/en-us/library/95hbf2ta.aspx