简体   繁体   English

ThreadPool.QueueUserWorkItem对新线程

[英]ThreadPool.QueueUserWorkItem Vs new Thread

I have the following code: 我有以下代码:

static void Main(string[] args)
{
    Console.Write("Press ENTER to start...");
    Console.ReadLine();

    Console.WriteLine("Scheduling work...");
    for (int i = 0; i < 1000; i++)
    {
        //ThreadPool.QueueUserWorkItem(_ =>
        new Thread(_ =>
            {
                Thread.Sleep(1000);
            }).Start();
    }
    Console.ReadLine();
}

According to the textbook C# 4.0 Unleashed by Bart De Smet (page 1466), using new Thread should mean using many more threads than if you use ThreadPool.QueueUserWorkItem which is commented out in my code. 根据Bart De Smet发布的C#4.0教科书(第1466页),使用新线程应该意味着使用的线程比使用我的代码中注释掉的ThreadPool.QueueUserWorkItem要多得多。 However I've tried both, and seen in Resource Monitor that with "new Thread", there are about 11 threads allocated, however when I use ThreadPool.QueueUserWorkItem, there are about 50. Why am I getting the opposite outcome of what is mentioned in this book? 但是我已经尝试了两种,并且在资源监视器中看到“新线程”,分配了大约11个线程,但是当我使用ThreadPool.QueueUserWorkItem时,大约有50个。为什么我得到的结果与提到的相反。在这本书?

Also why if you increase the sleep time, do you get many more threads allocated when using ThreadPool.QueueUserWorkItem? 另外,为什么如果增加睡眠时间,在使用ThreadPool.QueueUserWorkItem时是否会分配更多的线程?

new Thread() just creates a Thread object; new Thread()只创建一个Thread对象; you forgot to call Start() (which creates the actual thread that you see in resource monitor). 你忘了调用Start() (它创建你在资源监视器中看到的实际线程)。

Also, if you are looking at the number of threads after the sleep has completed, you won't see any of the new Thread s as they have already exited. 此外,如果您在睡眠完成后查看线程数,您将看不到任何new Thread因为它们已经退出。 On the other hand, the ThreadPool keeps threads around for some time so it can reuse them, so in that case you can still see the threads even after the sleep has completed. 另一方面, ThreadPool将线程保持一段时间以便它可以重用它们,因此在这种情况下,即使在睡眠完成后你仍然可以看到线程。

With new Thread() , you might be seeing the number staying around 160 because it took one second to start that many threads, so by the time the 161st thread is started, the first thread is already finished. 使用new Thread() ,您可能会看到数字保持在160左右,因为启动那么多线程需要一秒钟,因此在第161个线程启动时,第一个线程已经完成。 You should see a higher number of threads if you increase the sleep time. 如果增加睡眠时间,您应该看到更多的线程。

As for the ThreadPool , it is designed to use as few threads as possible while also keeping the CPU busy. 至于ThreadPool ,它被设计为使用尽可能少的线程,同时也保持CPU忙。 Ideally, the number of busy threads is equal to the number of CPU cores. 理想情况下,繁忙线程的数量等于CPU核心的数量。 However, if the pool detects that its threads are currently not using the CPU (sleeping, or waiting for another thread), it starts up more threads (at a rate of 1/second, up to some maximum) to keep the CPU busy. 但是,如果池检测到其线程当前未使用CPU(正在休眠或等待另一个线程),则会启动更多线程(速率为1 /秒,最高达到某个最大值)以保持CPU忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM