c# - Parallel.For(Foreach) 将创建多少个线程?默认 MaxDegreeOfParallelism?

标签 c# multithreading .net-4.5 parallel.foreach

我想知道,当我运行 Parallel.For/ForEach 循环时,将使用多少个线程。

我发现,它可以通过 MaxDegreeOfParallelism 选项进行更改。

MSDN 上的 MaxDegreeOfParallelism 帮助说 ( link ):

By default, For and ForEach will utilize however many threads the underlying scheduler provides, so changing MaxDegreeOfParallelism from the default only limits how many concurrent tasks will be used.

但我不知道底层调度器提供了多少线程。

我怎样才能找到它?

我可以用 9999999 次循环测试它,但是这个测试会显示数字,但不会显示确定该数字的规则。

稍后编辑/添加:

我用谷歌搜索“sheduler max concurrency”,我发现(在 MSDN - link ),TashSheduler 类有 MaximumConcurrencyLevel 属性,并且:

Returns an integer that represents the maximum concurrency level. The default scheduler returns Int32.MaxValue.

TaskSheduler 类用作这些并行循环的“底层调度程序”?

最佳答案

根据 MSDN :

The default scheduler for Task Parallel Library and PLINQ uses the .NET Framework ThreadPool to queue and execute work. In the .NET Framework 4, the ThreadPool uses the information that is provided by the System.Threading.Tasks.Task type to efficiently support the fine-grained parallelism (short-lived units of work) that parallel tasks and queries often represent.

查看 documentation ThreadPool,它说:

There is one thread pool per process. Beginning with the .NET Framework 4, the default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. A process can call the GetMaxThreads method to determine the number of threads. The number of threads in the thread pool can be changed by using the SetMaxThreads method.

关于c# - Parallel.For(Foreach) 将创建多少个线程?默认 MaxDegreeOfParallelism?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18943703/

相关文章:

visual-studio - VS 2012 .NET 4.5 中的 Entity Framework 构建问题

c# - 让编译器知道 Generic 有一个字段

c# - 如何在 WPF 中使模型类可观察

c++ - 用于在 C++ 中同步线程的二进制信号量

c# - 安装 VS2019 后无法在 VS2015 中构建 .NET 4.5

c# - 为 Unity 构造函数注入(inject)配置封闭泛型类型

c# - RabbitMQ 批量消费消息并一次性确认

c# - 自定义文本框自动完成

Ruby Redis - 线程性能

C# : Method name expected