Further to the issue with default JDK implementation with IteratorSpliterator, https://lwpro2.dev/2020/12/21/files-list-parallel-stream/
I have created a custom Spliterator to evenly split the stream and with threshold for the split size.
public class CacheSplitter<T> implements Spliterator<T> {
// private final Collection<T> collection;
private final T[] values;
private int start;
private int end;
private final int THRESHOLD;
public CacheSplitter(T[] values, int threshold) {
this(values, 0, values.length, threshold);
}
public CacheSplitter(T[] values, int start, int end, int threshold) {
this.values = values;
this.start = start;
this.end = end;
this.THRESHOLD = threshold;
}
@Override
public boolean tryAdvance(Consumer action) {
if(start< end){
action.accept(values[start++]);
return true;
}
return false;
}
@Override
public Spliterator trySplit() {
if(end - start < THRESHOLD){
return null;
}
int mid = (start + end)/2;
return new CacheSplitter(values, start, start= mid+1, THRESHOLD);
}
@Override
public long estimateSize() {
return end - start;
}
@Override
public int characteristics() {
return ORDERED | SIZED | SUBSIZED | NONNULL;
}
}
https://github.com/1wpro2/jdk-patch/blob/main/FixedSizeSpliterator.java