相同的线程创建3次而不是3个不同的线程

时间:2015-01-20 15:53:07

标签: c# multithreading

当我运行此代码时:

        threads = new Thread[length];

        for (int j = 0; j < length; j++)
        {
            threads[j] = new Thread(() =>
            {
                Console.WriteLine("Thread "+j+" has started!");
            });
            threads[j].Start();
        }
        for (int i = 0; i < length; i++)
        {
            threads[i].Join();
        }

为什么我得到这个输出:(如果长度= 3)

Thread 3 has started!
Thread 3 has started!
Thread 3 has started!

我不应该得到这样的输出:

Thread 0 has started!
Thread 1 has started!
Thread 2 has started!

我错过了什么?

0 个答案:

没有答案