1

我正在学习这个 Java 8 特性,我真的很难理解Spliterator接口的trySplit()方法实现,以防生成的并行处理的自定义类Stream

任何人都可以通过一个清晰的例子帮助我一些好的教程吗?

4

2 回答 2

1

理想的 trySplit 方法可以有效地(无需遍历)将其元素精确地分成两半,从而实现平衡的并行计算。许多偏离这一理想的做法仍然非常有效。例如,仅对近似平衡的树进行近似拆分,或者对于其中叶节点可能包含一个或两个元素的树,无法进一步拆分这些节点。然而,平衡中的大偏差和/或过度低效的 trySplit 机制通常会导致较差的并行性能。

以及带有注释的方法结构

 public Spliterator<T> trySplit() {
   int lo = origin; // divide range in half
   int mid = ((lo + fence) >>> 1) & ~1; // force midpoint to be even
   if (lo < mid) { // split out left half
     origin = mid; // reset this Spliterator's origin
     return new TaggedArraySpliterator<>(array, lo, mid);
   }
   else       // too small to split
     return null;
 }

如需更多曝光,请阅读https://docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html

于 2018-10-26T12:10:45.617 回答
-1

这是实现拆分器的示例。

public class Payment {
    private String category;
    private double amount;

    public Payment(double amount, String category) {
        this.amount = amount;
        this.category = category;
    }

    public String getCategory() {
        return category;
    }

    public double getAmount() {
        return amount;
    }
}

TrySplit 实现:

import java.util.List;
import java.util.Spliterator;
import java.util.function.Consumer;

public class PaymentBatchSpliterator implements Spliterator<Payment> {

    private List<Payment> paymentList;
    private int current;
    private int last;  // inclusive

    public PaymentBatchSpliterator(List<Payment> payments) {
        this.paymentList = payments;
        last = paymentList.size() - 1;
    }

    public PaymentBatchSpliterator(List<Payment> payments, int start, int last) {
        this.paymentList = payments;
        this.current = start;
        this.last = last;
    }

    @Override
    public boolean tryAdvance(Consumer<? super Payment> action) {
        if (current <= last) {
            action.accept(paymentList.get(current));
            current++;
            return true;
        }
        return false;
    }

    @Override
    public Spliterator<Payment> trySplit() {
        if ((last - current) < 100) {
            return null;
        }

        // first stab at finding a split position
        int splitPosition = current + (last - current) / 2;
        // if the categories are the same, we can't split here, as we are in the middle of a batch
        String categoryBeforeSplit = paymentList.get(splitPosition-1).getCategory();
        String categoryAfterSplit = paymentList.get(splitPosition).getCategory();

        // keep moving forward until we reach a split between categories
        while (categoryBeforeSplit.equals(categoryAfterSplit)) {
            splitPosition++;
            categoryBeforeSplit = categoryAfterSplit;
            categoryAfterSplit = paymentList.get(splitPosition).getCategory();
        }

        // safe to create a new spliterator
        PaymentBatchSpliterator secondHalf = new PaymentBatchSpliterator(paymentList,splitPosition,last);
        // reset our own last value
        last = splitPosition - 1;

        return secondHalf;
    }

    @Override
    public long estimateSize() {
        return last - current;
    }

    @Override
    public int characteristics() {
        return 0;
    }
}

是来自 GitHub参考的示例

于 2018-10-26T12:21:26.807 回答