本文介绍了螺纹VS线程池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用一个新的线程,并使用一个线程从线程池之间的区别是什么?什么性能优势在那里,我为什么要考虑使用一个线程池,而不是一个我已经明确创建? .NET的,我特别想在这里,但一般的例子都是精品。

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 the pool rather than one I've explicitly created? I'm thinking specifically of .NET here, but general examples are fine.

推荐答案

线程池将由

  • 在重新使用已创建创建新的,与其线程(一个昂贵的过程)
  • 节流线程创建的速度时,有一阵新的工作项目的请求(我相信这只是在.NET 3.5)

  • Reusing threads that have already been created instead of creating new ones (an expensive process)
  • Throttling the rate of thread creation when there is a burst of requests for new work items (I believe this is only in .NET 3.5)

  • 如果你排队100线程池任务已创建以服务这些请求(如10为例),它只会使用尽可能多的线程。线程池将频繁的检查(我相信3.5 SP1每500毫秒),如果有排队的任务,它会做一个新的线程。如果任务是快速,那么新的线程数目将是小的和重复使用10个左右的线程为短的任务将是比创建100个线程前面更快

  • If you queue 100 thread pool tasks, it will only use as many threads as have already been created to service these requests (say 10 for example). The thread pool will make frequent checks (I believe every 500ms in 3.5 SP1) and if there are queued tasks, it will make one new thread. If your tasks are quick, then the number of new threads will be small and reusing the 10 or so threads for the short tasks will be faster than creating 100 threads up front.

如果你的工作量一直有大量的线程池的请求进来,那么线程池将调整自己的工作量由上述过程中创造池中有更多的线程,以便有线程的数量较多可用来处理请求

If your workload consistently has large numbers of thread pool requests coming in, then the thread pool will tune itself to your workload by creating more threads in the pool by the above process so that there are a larger number of thread available to process requests

检查Here更多的关于如何线程池的功能深度信息引擎盖

check Here for more in depth info on how the thread pool functions under the hood

创建一个新的线程自己会更合适,如果这份工作就要被运行时间相对较长(大概在一两秒钟,但要看具体情况而定)

Creating a new thread yourself would be more appropriate if the job were going to be relatively long running (probably around a second or two, but it depends on the specific situation)

@Krzysztof - 线程池中的线程都是后台线程主线程结束时,将停止。手动创建的线程被默认前景(将保持运行主线程结束后),但对它们调用Start之前可设置为背景

@Krzysztof - Thread Pool threads are background threads that will stop when the main thread ends. Manually created threads are foreground by default (will keep running after the main thread has ended), but can be set to background before calling Start on them.

这篇关于螺纹VS线程池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 14:36