C#-QueuedTaskScheduler-threadCount与maxConcurrencyLevel

我从QueuedTaskScheduler.cs documentation中看到以下内容,但对我来说却不太清楚.

threadCount-创建并用于处理工作项的线程数.
maxConcurrencyLevel-此调度程序的工作允许的最大并发度.

问题-threadCount和& amp;之间有什么区别? maxConcurrencyLevel?

最佳答案
threadCount是为计划程序完成的任务创建的许多线程.这意味着您的应用程序将创建一堆具有给定限制的线程.

Thread control. The priorities, fairness, and concurrency level control all apply when QueuedTaskScheduler is used on top of another TaskScheduler as well as when used with dedicated threads for the scheduler. However, QueuedTaskScheduler also provides very low-level control over the threads utilized by the scheduler when dedicated threads are requested.

maxConcurrencyLevel是调度程序中同时执行的许多任务.这意味着您的调度程序将同时处理有限数量的任务.

Concurrency Levels. In a large system, you may want to control how much parallelism is afforded to different parts of the system. With parallel loops and PLINQ queries, you can control this on a per-loop or per-query basis, but out-of-the-box there’s no way to control it across loops, and there’s no built-in way to control it for tasks. By scheduling all related work to a TaskScheduler that enforces a maximum concurrency level, that functionality is gained.

这个数字看起来非常相似,但是却有些不同.从理论上讲,线程数应几乎等于应用程序服务器上的内核数,因为这可以帮助您避免上下文切换开销.

至于并发级别,如果您的任务非常小,那么此数目可能几乎等于线程数(或线程数1),也可能比线程数大得多,因此处理器将快速执行它们.

总而言之,您应该尝试不同的组合并评估系统性能.

您也可以在related post on MSDN blogs上看到一些示例

转载注明原文:C#-QueuedTaskScheduler-threadCount与maxConcurrencyLevel - 代码日志