我正在阅读Essential C# 5.0其中说,
The thread pool also assumes that all the work will be relatively short-running (that is, consuming milliseconds or seconds of processor time, not hours or days). By making this assumption, it can ensure that each processor is working full out on a task, and not inefficiently time slicing multiple tasks. The thread pool attempts to prevent excessive time slicing by ensuring that thread creation is "throttled" and so that no one processor is "oversubscrived" with too many threads.
我一直认为多线程的好处之一是时间切片。
如果您 >1 个处理器,则可以同时运行线程并实现真正的多线程。但除非是这种情况,否则您必须对具有多个线程的应用程序采取时间切片,对吧?
所以,如果C#中的ThreadPool没有时间片,那么,
a.这是否意味着线程池仅用于规避创建新线程的开销?
b.这是否意味着线程池不能同时运行多个线程,除非处理器有多个核心,每个核心可以运行一个进程?
最佳答案
.NET 线程池将为每个核心创建多个线程,但具有启发式方法,可以在执行最大工作量的同时保持尽可能低的线程数量。
这意味着如果您的代码受 CPU 限制,则最终可能每个核心只有一个线程。如果您的代码受 I/O 限制并且会阻塞或对大量工作项进行排队,则最终可能会在每个核心上拥有许多线程。
不仅仅是线程创建成本高昂:数百个线程之间的上下文切换会占用大量时间,您宁愿花在运行自己的代码上。更多线程几乎永远不会更好。
关于c# - 我对C#线程池的理解正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28389449/