为了账号安全,请及时绑定邮箱和手机立即绑定

Spliterator Java 8 - 自定义实现

Spliterator Java 8 - 自定义实现

拉风的咖菲猫 2021-12-10 10:18:28
我正在学习这个 Java 8 特性,我真的发现很难理解Spliterator接口的trySplit()方法实现,如果自定义类用于并行处理生成的Stream.任何人都可以用一个清晰的例子来帮助我提供一些好的教程吗?
查看完整描述

2 回答

?
有只小跳蛙

TA贡献1824条经验 获得超8个赞

理想的 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


查看完整回答
反对 回复 2021-12-10
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

这是实现 Spliterator 的示例。


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参考的示例


查看完整回答
反对 回复 2021-12-10
  • 2 回答
  • 0 关注
  • 155 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信