2

I have an implementation as below :

  Parallel.ForEach(dtJobs.AsEnumerable(),new ParallelOptions{MaxDegreeofParalellism=5},dataRow=>

  {

       some long running process
});

The Property MaxDegreeofParalellism=5 will create only 5 threads ?

Am i right ? If not please correct me..

Sai Avinash
  • 4,683
  • 17
  • 58
  • 96
  • It will create up to 5 threads. See http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism(v=vs.110).aspx – dotnetom Jun 24 '14 at 11:30

1 Answers1

3

From ParallelOptions.MaxDegreeOfParallelism:

The MaxDegreeOfParallelism property affects the number of concurrent operations run by Parallel method calls that are passed this ParallelOptions instance. A positive property value limits the number of concurrent operations to the set value. If it is -1, there is no limit on the number of concurrently running operations.

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.

So yes, it will max out at 5. It can't guarantee to get to 5, that depends on how many threads the underlying scheduler provides.

Community
  • 1
  • 1
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • can we use it for a long running process ? approx it takes 10 mins to complete – Sai Avinash Jun 24 '14 at 13:41
  • `Parallel.ForEach`? You should take into account that it uses the `ThreadPool` behind the scenes, and you dont really want to starve the ThreadPool with long running operations. I suggest you take a look at my answer to your previous question: http://stackoverflow.com/a/24381060/1870803 – Yuval Itzchakov Jun 24 '14 at 13:45