Why so much difference in performance between Thread and Task?

后端 未结 6 1416
独厮守ぢ
独厮守ぢ 2020-12-14 16:36

Windows 7, Intel CORE i3, 64 bit, RAM 4Gb, 2.27 GHz
.NET Framework 4.0

I have the following code:

static void Main(string[] args)
{
             


        
6条回答
  •  旧巷少年郎
    2020-12-14 16:53

    Every time you start a Task it goes into a pool to be served by a number of threads, many of which may be pre-created. There is an M:N ratio of tasks to threads in the pool.

    Every time you start a Thread it creates a new thread and all of the overhead associated with thread creation. Since you are explicitly creating a thread, there is a 1:1 ratio of threads.

    The closer the ratio of tasks to threads reaches 1, the "slower" task startup it will take. In reality, the ThreadPool ensures the ratio stays much higher than 1.

提交回复
热议问题