多少个线程的Parallel.For(Foreach源)将创建?默认MaxDegreeOfParallelism? [英] How many threads Parallel.For(Foreach) will create? Default MaxDegreeOfParallelism?

查看:1530
本文介绍了多少个线程的Parallel.For(Foreach源)将创建?默认MaxDegreeOfParallelism?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to know, how many threads will be used when I run Parallel.For/ForEach loop.

,我发现,它可以由MaxDegreeOfParallelism选项改变

I found, that it can be changed by MaxDegreeOfParallelism option.

在MSDN上MaxDegreeOfParallelism帮助说(<一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism.aspx\">link):

MaxDegreeOfParallelism help on MSDN says (link):

默认情况下,和foreach将利用然而,许多线程
  底层的调度程序提供,因此更改MaxDegreeOfParallelism从
  默认的唯一限制多少并发任务会被使用。

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.

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

But I don't know how many threads underlying scheduler provides.

我怎样才能找到呢?

我可以用9999999运行循环测试,但这个测试会告诉我一些,但不能决定这个数字的规则。

I could test it with loop with 9999999 runs, however this test will show me number, but not the rule that determine that number.

编辑/后来补充:

我用Google搜索sheduler最大并发,我发现(在MSDN - <一个href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskscheduler.maximumconcurrencylevel.aspx\">link),该 TashSheduler 类有 MaximumConcurrencyLevel 属性,

I googled for "sheduler max concurrency", and I found (at MSDN - link), that TashSheduler class has MaximumConcurrencyLevel property, and:

返回重新presents的最大并发级别的整数。该
  默认的调度返回Int32.MaxValue。

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

这TaskSheduler类作为基础调度这些并行循环?

推荐答案

根据 MSDN

有关任务并行库和PLINQ中的缺省调度使用.NET Framework 线程池来排队和执行工作。在.NET Framework 4中,线程池使用由提供的信息 System.Threading.Tasks.Task 键入有效支持细粒度并行(工作短命单位)并行任务和查询往往重present。

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.

看着线程池<的文档 / code>,它说:

Looking at the documentation of ThreadPool, it says:

有是每个进程一个线程池。与.NET框架4开始,对于处理线程池的默认大小取决于多种因素,诸如虚拟地址空间的大小。一个进程可以调用 GetMaxThreads 方法来确定线程数。线程池中的线程数可以使用 SetMaxThreads 方法来改变。

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.

这篇关于多少个线程的Parallel.For(Foreach源)将创建?默认MaxDegreeOfParallelism?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆