Multiple Parallel.ForEach loops in .Net

  • Does both my Parallel.ForEach loops use the same thread pool under the hood.

    Yes

  • How does Parallel.ForEach limits the threads with MaxDegreeOfParallelism.

    ParallelOptions.MaxDegreeOfParallelism Gets or sets the maximum number of concurrent tasks enabled by this ParallelOptions instance.

    By default, methods on the Parallel class attempt to use all available processors, are non-cancelable, and target the default TaskScheduler (TaskScheduler.Default). ParallelOptions enables overriding these defaults.

  • How multiple Parallel.ForEach loops and one managed thread pool work together?

    They share the same thread pool. As it is described here:

    Generally, you do not need to modify this setting. However, you may choose to set it explicitly in advanced usage scenarios such as these:

    When you’re running multiple algorithms concurrently and want to manually define how much of the system each algorithm can utilize. You can set a MaxDegreeOfParallelism value for each.

Leave a Comment