Thread vs ThreadPool

后端 未结 11 2216
鱼传尺愫
鱼传尺愫 2020-11-22 15:57

What is the difference between using a new thread and using a thread from the thread pool? What performance benefits are there and why should I consider using a thread from

11条回答
  •  悲哀的现实
    2020-11-22 16:24

    I was curios about the relative resource usage for these and and ran a benchmark on my 2012 dual-core Intel i5 laptop using .net 4.0 release build on windows 8. Thread Pools took on average 0.035ms to start where Threads took an average of 5.06ms. In other words Thread in the pool started about 300x faster for large numbers of short lived threads. At least in the tested range (100-2000) threads, the total time per thread seemed pretty constant.

    This is the code that was benchmarked:

        for (int i = 0; i < ThreadCount; i++) {
            Task.Run(() => { });
        }
    
        for (int i = 0; i < ThreadCount; i++) {
            var t = new Thread(() => { });
            t.Start();
        }
    

提交回复
热议问题