我想知道,当我运行 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, theThreadPool
uses the information that is provided by theSystem.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 theSetMaxThreads
method.
关于c# - Parallel.For(Foreach) 将创建多少个线程?默认 MaxDegreeOfParallelism?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18943703/