我正在下载文件,这意味着即使我只有 2 个内核,创建两个以上的线程也会提高性能。
是否Parallel.ForEach()
只制作与内核数量一样多的线程,
即使我将 ParallelOptions.MaxDegreeOfParallelism 设置为更高的值?
最佳答案
从文档:
The MaxDegreeOfParallelism limits the number of concurrent operations run by Parallel method calls that are passed this ParallelOptions instance to the set value, if it is positive. If MaxDegreeOfParallelism is -1, then there is no limit placed on the number of concurrently running operations.
这个函数只抛出
ArgumentOutOfRangeException
和 MaxDegreeOfParalleism
的类型是整数。所以这意味着,它可以创建比内核数更多的线程。我可以确认,因为我已经运行了很多 xml 生成过程,并且任务管理器显示的进程数与我之前设置的完全相同。
关于.net - Parallel.ForEach() - 多少个线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12933308/