views:

1441

answers:

3

If

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

All 1000 Threads will spawn almost simultaneously?

+1  A: 

It works out an optimal number of threads based on the number of processors/cores. They will not all spawn at once.

Colin Mackay
+9  A: 

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.

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.

Jon Skeet
Besides starting with a "No." I think that this answer points to a Yes to the title question. The "No" is probably directed to the final question in the question description.
Jader Dias
Yes indeed - I'll edit to make it clearer.
Jon Skeet
+2  A: 

See Does Parallel.For use one Task per iteration? for an idea of a "mental model" to use. However the author does state that "At the end of the day, it’s important to remember that implementation details may change at any time."

Kevin Hakanson