简体   繁体   中英

What is the equivalent of Spliterator in C#?

In Java I can write a Spliterator interface implementation, in which I can specify how exactly a source collection will be split into subcollections for parallel processing.

But I don't understand how to do this in C#, all I know is that you can call AsParallel on IEnumerable , but can you control the splitting process?

For example, my source collection is an array and I want to split it into 5 item subarrays, and want linq to work with those subarrays. Is this possible?

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));

Currently C# dose not offer this feature like Java. Parallel tasks in C# have a MaxDegreeOfParallelism parameter which let you specify maximum number of concurrent tasks enabled by ParallelOptions instance and it automatically handle number of tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM