gpt4 book ai didi

java - Spliterator Java 8 - 自定义实现

转载 作者:太空宇宙 更新时间:2023-11-04 10:05:20 24 4
gpt4 key购买 nike

我正在学习这个 Java 8 功能,但我真的发现很难理解 Spliterator接口(interface)的trySplit()自定义类的方法实现用于并行处理生成的Stream .

有人可以帮我提供一些带有清晰示例的好教程吗?

最佳答案

An ideal trySplit method efficiently (without traversal) divides its elements exactly in half, allowing balanced parallel computation. Many departures from this ideal remain highly effective; for example, only approximately splitting an approximately balanced tree, or for a tree in which leaf nodes may contain either one or two elements, failing to further split these nodes. However, large deviations in balance and/or overly inefficient trySplit mechanics typically result in poor parallel performance.

以及带注释的方法结构

 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

关于java - Spliterator Java 8 - 自定义实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53008076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com