线程池与线程生成 [英] Thread Pool vs Thread Spawning

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

问题描述

有人可以列出线程生成与线程池之间的一些比较点,哪个比较好?请考虑将.NET框架作为支持两者的参考实现.

Can someone list some comparison points between Thread Spawning vs Thread Pooling, which one is better? Please consider the .NET framework as a reference implementation that supports both.

推荐答案

线程池线程比常规线程便宜得多,它们可以池化线程所需的系统资源.但是它们有很多局限性,可能会使其不适应:

Thread pool threads are much cheaper than a regular Thread, they pool the system resources required for threads. But they have a number of limitations that may make them unfit:

  • 您不能中止线程池线程
  • 没有简单的方法来检测线程池是否完成,没有Thread.Join()
  • 没有简单的方法可以封送线程池线程中的异常
  • 除了消息框之外,您无法在线程池线程上显示任何类型的UI
  • 线程池线程的运行时间不应超过几秒钟
  • 线程池线程不应长时间阻塞

后两个约束是线程池调度程序的副作用,它试图将活动线程数限制为CPU可用的内核数.如果您安排许多经常阻塞的长时间运行的线程,则可能会导致长时间的延迟.

The latter two constraints are a side-effect of the threadpool scheduler, it tries to limit the number of active threads to the number of cores your CPU has available. This can cause long delays if you schedule many long running threads that block often.

许多其他线程池实现都有类似的约束,即给予或接受.

Many other threadpool implementations have similar constraints, give or take.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆