为什么我的线程池的线程没有同时完成

why my threadpool 's threads do not finish at same time

在主函数中,我写了这段代码:

            ThreadPool.SetMaxThreads(200, 200);

        for (int i = 0; i < 100; i++)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadWhichWillCallSQL_test), i);
            Thread.Sleep(1);
        }

线程函数如下:

        static public void ThreadWhichWillCallSQL_test(Object o1)
    {
        Thread.Sleep(5000);
        Console.WriteLine(DateTime.Now.ToString()); 
        return;
    }

输出是:

    5/25/2017 2:00:01 PM
5/25/2017 2:00:01 PM
5/25/2017 2:00:01 PM
5/25/2017 2:00:01 PM
5/25/2017 2:00:02 PM
5/25/2017 2:00:03 PM
5/25/2017 2:00:04 PM
5/25/2017 2:00:05 PM
5/25/2017 2:00:05 PM
5/25/2017 2:00:06 PM
5/25/2017 2:00:06 PM
5/25/2017 2:00:06 PM
5/25/2017 2:00:06 PM
5/25/2017 2:00:07 PM
5/25/2017 2:00:07 PM
5/25/2017 2:00:08 PM
5/25/2017 2:00:08 PM
5/25/2017 2:00:09 PM
5/25/2017 2:00:09 PM
5/25/2017 2:00:10 PM
5/25/2017 2:00:10 PM
5/25/2017 2:00:10 PM
5/25/2017 2:00:11 PM
5/25/2017 2:00:11 PM
5/25/2017 2:00:11 PM
5/25/2017 2:00:11 PM
5/25/2017 2:00:12 PM
5/25/2017 2:00:12 PM
5/25/2017 2:00:12 PM
5/25/2017 2:00:13 PM
5/25/2017 2:00:13 PM
5/25/2017 2:00:13 PM
5/25/2017 2:00:14 PM
5/25/2017 2:00:14 PM
5/25/2017 2:00:14 PM
5/25/2017 2:00:15 PM
5/25/2017 2:00:15 PM
5/25/2017 2:00:15 PM
5/25/2017 2:00:15 PM
5/25/2017 2:00:16 PM
5/25/2017 2:00:16 PM
5/25/2017 2:00:16 PM
5/25/2017 2:00:16 PM
5/25/2017 2:00:17 PM
5/25/2017 2:00:17 PM
5/25/2017 2:00:17 PM
5/25/2017 2:00:17 PM
5/25/2017 2:00:18 PM
5/25/2017 2:00:18 PM
5/25/2017 2:00:18 PM
5/25/2017 2:00:18 PM
5/25/2017 2:00:19 PM
5/25/2017 2:00:19 PM
5/25/2017 2:00:19 PM
5/25/2017 2:00:19 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:20 PM
5/25/2017 2:00:21 PM
5/25/2017 2:00:21 PM
5/25/2017 2:00:21 PM
5/25/2017 2:00:21 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:22 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:23 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:24 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:25 PM
5/25/2017 2:00:26 PM
5/25/2017 2:00:26 PM
5/25/2017 2:00:26 PM
5/25/2017 2:00:26 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:27 PM
5/25/2017 2:00:28 PM
5/25/2017 2:00:28 PM
5/25/2017 2:00:28 PM
5/25/2017 2:00:28 PM
5/25/2017 2:00:28 PM

大家可以看到所有线程完成的整个过程持续了将近28秒,据我了解,这100个线程也许不可能同时完成,但也不可能相差那么多时间。

我也设置了

ThreadPool.SetMaxThreads(200, 200);

并且只分配了 100 个线程,所以不应该有任何线程等待其他线程停止,对吧?

这里是完整代码

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Threading;

    namespace ThreadpoolDelay
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    ThreadPool.SetMaxThreads(200, 200);

                    for (int i = 0; i < 30; i++)
                    {
                        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadWhichWillCallSQL_test), i);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

                Console.WriteLine("\nPress ENTER to continue...");
                Console.Read();
            }
            public static void ThreadWhichWillCallSQL_test(Object o1)
            {
                Thread.Sleep(5000);
                Console.WriteLine(DateTime.Now.ToString());
                return;
            }

        }
    }

好问题。

thread pull 中线程的执行取决于很多因素,其中一个因素是 cpu core 。它把线程放到每个核心的 运行 中,一旦完成它就放另一个。

示例:如果有 4 个核心 cpu。它同时并行放置 4 个线程,然后将下一个线程作为线程完成任务。机制有些像那样,不完全是那样。 你也有 5 秒的延迟(线程睡眠)所以 如果有 4 个核心,计算就像这样:(30 thread/4 核心)*5 一些这样的不完全是。

你也可以在那里看到更多信息:https://msdn.microsoft.com/en-us/library/3dasc8as(v=vs.80).aspx

在您的示例中,线程没有同时启动。在 SetMaxThreads

之后添加以下调用
ThreadPool.SetMinThreads(200, 200);

这将使线程同时启动。这与 ThreadPool 线程创建机制有关。我建议阅读 ThreadPool documentation 了解详情。

为了更正确地调试它,请更新您的 ThreadWhichWillCallSQL_test 方法以添加开始时间:

    static void ThreadWhichWillCallSQL_test(object o1)
    {
        Console.WriteLine("start:"  + DateTime.Now);
        Thread.Sleep(5000);
        Console.WriteLine("finish: " + DateTime.Now);
    }