是否Parallel.ForEach限制活动线程的数量? [英] Does Parallel.ForEach limits the number of active threads?

查看:760
本文介绍了是否Parallel.ForEach限制活动线程的数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果

var arrayStrings = new string[1000];
Parallel.ForEach<string>(arrayStrings, someString =>
{
    DoSomething(someString);
});

所有1000主题将几乎同时产卵?

All 1000 Threads will spawn almost simultaneously?

推荐答案

没有,也不会启动1000年主题 - 是的,它会限制多少线程使用。并行扩展使用内核适当数量的基础上,有多少你实际拥有的的多少都已经忙。它每个核心分配工作,然后使用一个名为工作窃取技术的有效地让每个线程处理自己的队列,只需要做任何昂贵的跨线程访问时,它真正需要的。

No, it won't start 1000 threads - yes, it will limit how many threads are used. Parallel Extensions uses an appropriate number of cores, based on how many you physically have and how many are already busy. It allocates work for each core and then uses a technique called work stealing to let each thread process its own queue efficiently and only need to do any expensive cross-thread access when it really needs to.

有一个看 PFX团队博客获得的负荷的有关如何分配工作和其他各种话题。

Have a look at the PFX Team Blog for loads of information about how it allocates work and all kinds of other topics.

请注意,在某些情况下,你可以指定并行你想要的程度了。

Note that in some cases you can specify the degree of parallelism you want, too.

这篇关于是否Parallel.ForEach限制活动线程的数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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