If you have specific partitioning requirements, you should have a look at Custom Partitioners for PLINQ and TPL and How to: Implement a Partitioner for Static Partitioning. These give an example of how you can implement a Partitioner<TSource>
, which is presumably similar to Java's Spliterator
.
However, most of the time, it's more beneficial to let the framework pick its own dynamic partitioning strategy. If your requirement is to limit the level of concurrency to 5, you can use WithDegreeOfParallelism
:
var summary = ints
.AsParallel()
.WithDegreeOfParallelism(5)
.Aggregate(
seed: (
count: 0,
sum: 0,
min: int.MaxValue,
max: int.MinValue),
updateAccumulatorFunc: (acc, x) => (
count: acc.count + 1,
sum: acc.sum + x,
min: Math.Min(acc.min, x),
max: Math.Max(acc.max, x)),
combineAccumulatorsFunc: (acc1, acc2) => (
count: acc1.count + acc2.count,
sum: acc1.sum + acc2.sum,
min: Math.Min(acc1.min, acc2.min),
max: Math.Max(acc1.max, acc2.max)),
resultSelector: acc => (
acc.count,
acc.sum,
acc.min,
acc.max,
avg: (double)acc.sum / acc.count));