com.landawn.abacus.util.stream.ParallelIteratorStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* Copyright (C) 2016, 2017, 2018, 2019 HaiYang Li
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.landawn.abacus.util.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Deque;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicLong;
import com.landawn.abacus.util.AsyncExecutor;
import com.landawn.abacus.util.ByteIterator;
import com.landawn.abacus.util.CharIterator;
import com.landawn.abacus.util.ContinuableFuture;
import com.landawn.abacus.util.DoubleIterator;
import com.landawn.abacus.util.FloatIterator;
import com.landawn.abacus.util.Fn.Suppliers;
import com.landawn.abacus.util.IntIterator;
import com.landawn.abacus.util.LongIterator;
import com.landawn.abacus.util.Multimap;
import com.landawn.abacus.util.Multiset;
import com.landawn.abacus.util.MutableBoolean;
import com.landawn.abacus.util.MutableLong;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Nth;
import com.landawn.abacus.util.ObjIterator;
import com.landawn.abacus.util.Pair;
import com.landawn.abacus.util.ShortIterator;
import com.landawn.abacus.util.Try;
import com.landawn.abacus.util.u.Holder;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.function.BiConsumer;
import com.landawn.abacus.util.function.BiFunction;
import com.landawn.abacus.util.function.BinaryOperator;
import com.landawn.abacus.util.function.Consumer;
import com.landawn.abacus.util.function.Function;
import com.landawn.abacus.util.function.Predicate;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.function.ToByteFunction;
import com.landawn.abacus.util.function.ToCharFunction;
import com.landawn.abacus.util.function.ToDoubleFunction;
import com.landawn.abacus.util.function.ToFloatFunction;
import com.landawn.abacus.util.function.ToIntFunction;
import com.landawn.abacus.util.function.ToLongFunction;
import com.landawn.abacus.util.function.ToShortFunction;
import com.landawn.abacus.util.function.TriFunction;
/**
*
*/
final class ParallelIteratorStream extends IteratorStream {
private final int maxThreadNum;
private final Splitor splitor;
private final AsyncExecutor asyncExecutor;
private volatile IteratorStream sequential;
ParallelIteratorStream(final Iterator extends T> values, final boolean sorted, final Comparator super T> comparator, final int maxThreadNum,
final Splitor splitor, final AsyncExecutor asyncExector, final Collection closeHandlers) {
super(values, sorted, comparator, closeHandlers);
this.maxThreadNum = checkMaxThreadNum(maxThreadNum);
this.splitor = splitor == null ? DEFAULT_SPLITOR : splitor;
this.asyncExecutor = asyncExector == null ? DEFAULT_ASYNC_EXECUTOR : asyncExector;
}
ParallelIteratorStream(final Stream stream, final boolean sorted, final Comparator super T> comparator, final int maxThreadNum, final Splitor splitor,
final AsyncExecutor asyncExector, final Deque closeHandlers) {
this(stream.iterator(), sorted, comparator, maxThreadNum, splitor, asyncExector, mergeCloseHandlers(stream, closeHandlers));
}
@Override
public Stream filter(final Predicate super T> predicate) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.filter(predicate);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private boolean hasNext = false;
@Override
public boolean hasNext() {
if (hasNext == false) {
while (true) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
break;
}
}
if (predicate.test(next)) {
hasNext = true;
break;
}
}
}
return hasNext;
}
@Override
public T next() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public Stream takeWhile(final Predicate super T> predicate) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.takeWhile(predicate);
}
final List> iters = new ArrayList<>(maxThreadNum);
final MutableBoolean hasMore = MutableBoolean.of(true);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private boolean hasNext = false;
@Override
public boolean hasNext() {
if (hasNext == false && hasMore.isTrue()) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
hasNext = true;
} else {
hasMore.setFalse();
}
}
if (hasNext && predicate.test(next) == false) {
hasNext = false;
hasMore.setFalse();
}
}
return hasNext;
}
@Override
public T next() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public Stream dropWhile(final Predicate super T> predicate) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.dropWhile(predicate);
}
final List> iters = new ArrayList<>(maxThreadNum);
final MutableBoolean dropped = MutableBoolean.of(false);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private boolean hasNext = false;
@Override
public boolean hasNext() {
if (hasNext == false) {
// Only one thread is kept for running after it's dropped.
if (dropped.isTrue()) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
hasNext = true;
}
}
} else {
while (dropped.isFalse()) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
break;
}
}
if (predicate.test(next) == false) {
hasNext = true;
dropped.setTrue();
break;
}
}
if (hasNext == false && dropped.isTrue()) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
hasNext = true;
}
}
}
}
}
return hasNext;
}
@Override
public T next() {
if (hasNext == false && hasNext() == false) {
throw new NoSuchElementException();
}
hasNext = false;
return next;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public Stream map(final Function super T, ? extends R> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.map(mapper);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public R next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
R result = mapper.apply((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
// @Override
// public Stream biMap(final BiFunction super T, ? super T, ? extends R> mapper, final boolean ignoreNotPaired) {
// if (maxThreadNum <= 1) {
// return new ParallelIteratorStream<>(sequential().biMap(mapper, ignoreNotPaired).iterator(), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
// }
//
// final List> iters = new ArrayList<>(maxThreadNum);
//
// for (int i = 0; i < maxThreadNum; i++) {
// iters.add(new ObjIteratorEx() {
// private Object pre = NONE;
// private Object next = NONE;
//
// @Override
// public boolean hasNext() {
// if (pre == NONE) {
// synchronized (elements) {
// if (elements.hasNext()) {
// pre = elements.next();
//
// if (elements.hasNext()) {
// next = elements.next();
// }
// }
// }
// }
//
// return ignoreNotPaired ? next != NONE : pre != NONE;
// }
//
// @Override
// public R next() {
// if (next == NONE && hasNext() == false) {
// throw new NoSuchElementException();
// }
//
// final R result = mapper.apply((T) pre, next == NONE ? null : (T) next);
// pre = NONE;
// next = NONE;
// return result;
// }
// });
// }
//
// return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
// }
//
// @Override
// public Stream triMap(final TriFunction super T, ? super T, ? super T, ? extends R> mapper, final boolean ignoreNotPaired) {
// if (maxThreadNum <= 1) {
// return new ParallelIteratorStream<>(sequential().triMap(mapper, ignoreNotPaired).iterator(), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
// }
//
// final List> iters = new ArrayList<>(maxThreadNum);
//
// for (int i = 0; i < maxThreadNum; i++) {
// iters.add(new ObjIteratorEx() {
// private Object prepre = NONE;
// private Object pre = NONE;
// private Object next = NONE;
//
// @Override
// public boolean hasNext() {
// if (prepre == NONE) {
// synchronized (elements) {
// if (elements.hasNext()) {
// prepre = elements.next();
//
// if (elements.hasNext()) {
// pre = elements.next();
//
// if (elements.hasNext()) {
// next = elements.next();
// }
// }
// }
// }
// }
//
// return ignoreNotPaired ? next != NONE : prepre != NONE;
// }
//
// @Override
// public R next() {
// if (next == NONE && hasNext() == false) {
// throw new NoSuchElementException();
// }
//
// final R result = mapper.apply((T) prepre, pre == NONE ? null : (T) pre, next == NONE ? null : (T) next);
// prepre = NONE;
// pre = NONE;
// next = NONE;
// return result;
// }
// });
// }
//
// return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
// }
@Override
public Stream slidingMap(final BiFunction super T, ? super T, R> mapper, final int increment, final boolean ignoreNotPaired) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().slidingMap(mapper, increment).iterator(), false, null, maxThreadNum, splitor, asyncExecutor,
closeHandlers);
}
final int windowSize = 2;
checkArgPositive(increment, "increment");
final List> iters = new ArrayList<>(maxThreadNum);
final MutableBoolean isFirst = MutableBoolean.of(true);
final Holder prev = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
@SuppressWarnings("unchecked")
private final T NONE = (T) StreamBase.NONE;
private T first = NONE;
private T second = NONE;
@Override
public boolean hasNext() {
if (first == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
if (increment > windowSize && isFirst.isFalse()) {
int skipNum = increment - windowSize;
while (skipNum-- > 0 && elements.hasNext()) {
elements.next();
}
}
if (elements.hasNext()) {
if (increment == 1) {
first = isFirst.isTrue() ? elements.next() : prev.value();
second = elements.hasNext() ? elements.next() : null;
prev.setValue(second);
} else {
first = elements.next();
second = elements.hasNext() ? elements.next() : null;
}
}
isFirst.setFalse();
}
}
}
return ignoreNotPaired ? second != NONE : first != NONE;
}
@Override
public R next() {
if ((ignoreNotPaired ? second == NONE : first == NONE) && hasNext() == false) {
throw new NoSuchElementException();
}
final R result = mapper.apply(first, second == NONE ? null : second);
first = NONE;
second = NONE;
return result;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public Stream slidingMap(final TriFunction super T, ? super T, ? super T, R> mapper, final int increment, final boolean ignoreNotPaired) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().slidingMap(mapper, increment).iterator(), false, null, maxThreadNum, splitor, asyncExecutor,
closeHandlers);
}
final int windowSize = 3;
checkArgPositive(increment, "increment");
final List> iters = new ArrayList<>(maxThreadNum);
final MutableBoolean isFirst = MutableBoolean.of(true);
final Holder prev = new Holder<>();
final Holder prev2 = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
@SuppressWarnings("unchecked")
private final T NONE = (T) StreamBase.NONE;
private T first = NONE;
private T second = NONE;
private T third = NONE;
@Override
public boolean hasNext() {
if (first == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
if (increment > windowSize && isFirst.isFalse()) {
int skipNum = increment - windowSize;
while (skipNum-- > 0 && elements.hasNext()) {
elements.next();
}
}
if (elements.hasNext()) {
if (increment == 1) {
first = isFirst.isTrue() ? elements.next() : prev2.value();
second = isFirst.isTrue() ? (elements.hasNext() ? elements.next() : null) : prev.value();
third = elements.hasNext() ? elements.next() : null;
prev2.setValue(second);
prev.setValue(third);
} else if (increment == 2) {
first = isFirst.isTrue() ? elements.next() : prev.value();
second = elements.hasNext() ? elements.next() : null;
third = elements.hasNext() ? elements.next() : null;
prev.setValue(third);
} else {
first = elements.next();
second = elements.hasNext() ? elements.next() : null;
third = elements.hasNext() ? elements.next() : null;
}
}
isFirst.setFalse();
}
}
}
return ignoreNotPaired ? third != NONE : first != NONE;
}
@Override
public R next() {
if ((ignoreNotPaired ? third == NONE : first == NONE) && hasNext() == false) {
throw new NoSuchElementException();
}
final R result = mapper.apply(first, second == NONE ? null : second, third == NONE ? null : third);
first = NONE;
second = NONE;
third = NONE;
return result;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public Stream mapFirstOrElse(final Function super T, ? extends R> mapperForFirst, final Function super T, ? extends R> mapperForElse) {
checkArgNotNull(mapperForFirst);
checkArgNotNull(mapperForElse);
if (maxThreadNum <= 1) {
return super.mapFirstOrElse(mapperForFirst, mapperForElse);
}
if (elements.hasNext()) {
final Function mapperForFirst2 = (Function) mapperForFirst;
final Function mapperForElse2 = (Function) mapperForElse;
final T first = elements.next();
return map(mapperForElse2).prepend(Stream.of(first).map(mapperForFirst2));
} else {
return (Stream) this;
}
}
@Override
public Stream mapLastOrElse(final Function super T, ? extends R> mapperForLast, final Function super T, ? extends R> mapperForElse) {
checkArgNotNull(mapperForLast);
checkArgNotNull(mapperForElse);
if (maxThreadNum <= 1) {
return super.mapLastOrElse(mapperForLast, mapperForElse);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
private boolean isLast = false;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
if (elements.hasNext() == false) {
isLast = true;
}
}
}
}
return next != NONE;
}
@Override
public R next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
final R result = isLast ? mapperForLast.apply((T) next) : mapperForElse.apply((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public CharStream mapToChar(final ToCharFunction super T> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.mapToChar(mapper);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public Character next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Character result = mapper.applyAsChar((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorCharStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public ByteStream mapToByte(final ToByteFunction super T> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.mapToByte(mapper);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public Byte next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Byte result = mapper.applyAsByte((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorByteStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public ShortStream mapToShort(final ToShortFunction super T> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.mapToShort(mapper);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public Short next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Short result = mapper.applyAsShort((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorShortStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public IntStream mapToInt(final ToIntFunction super T> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.mapToInt(mapper);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public Integer next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Integer result = mapper.applyAsInt((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorIntStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public LongStream mapToLong(final ToLongFunction super T> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.mapToLong(mapper);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public Long next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Long result = mapper.applyAsLong((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorLongStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public FloatStream mapToFloat(final ToFloatFunction super T> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.mapToFloat(mapper);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public Float next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Float result = mapper.applyAsFloat((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorFloatStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public DoubleStream mapToDouble(final ToDoubleFunction super T> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.mapToDouble(mapper);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public Double next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
Double result = mapper.applyAsDouble((T) next);
next = NONE;
return result;
}
});
}
return new ParallelIteratorDoubleStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public Stream flatMap(final Function super T, ? extends Stream extends R>> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorStream<>(sequential().flatMap(mapper), false, null, maxThreadNum, splitor, asyncExecutor, null);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private Iterator extends R> cur = null;
private Stream extends R> s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Deque tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public R next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.next();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
final Deque newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalArrayDeque(1)
: new LocalArrayDeque(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, newCloseHandlers);
}
@Override
public CharStream flatMapToChar(final Function super T, ? extends CharStream> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorCharStream(sequential().flatMapToChar(mapper), false, maxThreadNum, splitor, asyncExecutor, null);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private CharIterator cur = null;
private CharStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Deque tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Character next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextChar();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
final Deque newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalArrayDeque(1)
: new LocalArrayDeque(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorCharStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, newCloseHandlers);
}
@Override
public ByteStream flatMapToByte(final Function super T, ? extends ByteStream> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorByteStream(sequential().flatMapToByte(mapper), false, maxThreadNum, splitor, asyncExecutor, null);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private ByteIterator cur = null;
private ByteStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Deque tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Byte next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextByte();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
final Deque newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalArrayDeque(1)
: new LocalArrayDeque(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorByteStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, newCloseHandlers);
}
@Override
public ShortStream flatMapToShort(final Function super T, ? extends ShortStream> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorShortStream(sequential().flatMapToShort(mapper), false, maxThreadNum, splitor, asyncExecutor, null);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private ShortIterator cur = null;
private ShortStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Deque tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Short next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextShort();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
final Deque newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalArrayDeque(1)
: new LocalArrayDeque(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorShortStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, newCloseHandlers);
}
@Override
public IntStream flatMapToInt(final Function super T, ? extends IntStream> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorIntStream(sequential().flatMapToInt(mapper), false, maxThreadNum, splitor, asyncExecutor, null);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private IntIterator cur = null;
private IntStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Deque tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Integer next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextInt();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
final Deque newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalArrayDeque(1)
: new LocalArrayDeque(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorIntStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, newCloseHandlers);
}
@Override
public LongStream flatMapToLong(final Function super T, ? extends LongStream> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorLongStream(sequential().flatMapToLong(mapper), false, maxThreadNum, splitor, asyncExecutor, null);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private LongIterator cur = null;
private LongStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Deque tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Long next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextLong();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
final Deque newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalArrayDeque(1)
: new LocalArrayDeque(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorLongStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, newCloseHandlers);
}
@Override
public FloatStream flatMapToFloat(final Function super T, ? extends FloatStream> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorFloatStream(sequential().flatMapToFloat(mapper), false, maxThreadNum, splitor, asyncExecutor, null);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private FloatIterator cur = null;
private FloatStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Deque tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Float next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextFloat();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
final Deque newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalArrayDeque(1)
: new LocalArrayDeque(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorFloatStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, newCloseHandlers);
}
@Override
public DoubleStream flatMapToDouble(final Function super T, ? extends DoubleStream> mapper) {
assertNotClosed();
if (maxThreadNum <= 1) {
return new ParallelIteratorDoubleStream(sequential().flatMapToDouble(mapper), false, maxThreadNum, splitor, asyncExecutor, null);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private T next = null;
private DoubleIterator cur = null;
private DoubleStream s = null;
private Runnable closeHandle = null;
@Override
public boolean hasNext() {
while ((cur == null || cur.hasNext() == false) && next != NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
next = (T) NONE;
break;
}
}
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
s = mapper.apply(next);
if (N.notNullOrEmpty(s.closeHandlers)) {
final Deque tmp = s.closeHandlers;
closeHandle = new Runnable() {
@Override
public void run() {
Stream.close(tmp);
}
};
}
cur = s.iterator();
}
return cur != null && cur.hasNext();
}
@Override
public Double next() {
if ((cur == null || cur.hasNext() == false) && hasNext() == false) {
throw new NoSuchElementException();
}
return cur.nextDouble();
}
@Override
public void close() {
if (closeHandle != null) {
final Runnable tmp = closeHandle;
closeHandle = null;
tmp.run();
}
}
});
}
final Deque newCloseHandlers = N.isNullOrEmpty(closeHandlers) ? new LocalArrayDeque(1)
: new LocalArrayDeque(closeHandlers);
newCloseHandlers.add(new Runnable() {
@Override
public void run() {
Stream.close(iters);
}
});
return new ParallelIteratorDoubleStream(Stream.parallelConcatt(iters, iters.size()), false, maxThreadNum, splitor, asyncExecutor, newCloseHandlers);
}
@Override
public Stream peek(final Consumer super T> action) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.peek(action);
}
final List> iters = new ArrayList<>(maxThreadNum);
for (int i = 0; i < maxThreadNum; i++) {
iters.add(new ObjIteratorEx() {
private Object next = NONE;
@Override
public boolean hasNext() {
if (next == NONE) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
}
}
}
return next != NONE;
}
@Override
public T next() {
if (next == NONE && hasNext() == false) {
throw new NoSuchElementException();
}
final T result = (T) next;
action.accept(result);
next = NONE;
return result;
}
});
}
return new ParallelIteratorStream<>(Stream.parallelConcatt(iters, iters.size()), false, null, maxThreadNum, splitor, asyncExecutor, closeHandlers);
}
@Override
public void forEach(final Try.Consumer super T, E> action, final Try.Runnable onComplete) throws E, E2 {
assertNotClosed();
if (maxThreadNum <= 1) {
super.forEach(action, onComplete);
return;
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Try.Runnable() {
@Override
public void run() {
T next = null;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
break;
}
}
action.accept(next);
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
try {
complette(futureList, eHolder, (E) null);
onComplete.run();
} finally {
close();
}
}
@Override
public void forEach(final Try.Function super T, ? extends Collection, E> flatMapper,
final Try.BiConsumer super T, ? super U, E2> action) throws E, E2 {
assertNotClosed();
if (maxThreadNum <= 1) {
super.forEach(flatMapper, action);
return;
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Try.Runnable() {
@Override
public void run() {
Collection c = null;
T next = null;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
break;
}
}
c = flatMapper.apply(next);
if (N.notNullOrEmpty(c)) {
for (U u : c) {
action.accept(next, u);
}
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
try {
complette(futureList, eHolder, (E) null);
} finally {
close();
}
}
@Override
public void forEach(
final Try.Function super T, ? extends Collection, E> flatMapper, final Try.Function super T2, ? extends Collection, E2> flatMapper2,
final Try.TriConsumer super T, ? super T2, ? super T3, E3> action) throws E, E2, E3 {
assertNotClosed();
if (maxThreadNum <= 1) {
super.forEach(flatMapper, flatMapper2, action);
return;
}
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Try.Runnable() {
@Override
public void run() {
Collection c2 = null;
Collection c3 = null;
T next = null;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
break;
}
}
c2 = flatMapper.apply(next);
if (N.notNullOrEmpty(c2)) {
for (T2 t2 : c2) {
c3 = flatMapper2.apply(t2);
if (N.notNullOrEmpty(c3)) {
for (T3 t3 : c3) {
action.accept(next, t2, t3);
}
}
}
}
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
try {
complette(futureList, eHolder, (E) null);
} finally {
close();
}
}
@Override
public void forEachPair(final Try.BiConsumer super T, ? super T, E> action, final int increment) throws E {
assertNotClosed();
if (maxThreadNum <= 1) {
super.forEachPair(action, increment);
return;
}
final int windowSize = 2;
checkArgPositive(increment, "increment");
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final Holder prev = new Holder<>();
final MutableBoolean isFirst = MutableBoolean.of(true);
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Try.Runnable() {
private T first = null;
private T second = null;
@Override
public void run() {
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
if (increment > windowSize && isFirst.isFalse()) {
int skipNum = increment - windowSize;
while (skipNum-- > 0 && elements.hasNext()) {
elements.next();
}
if (elements.hasNext() == false) {
break;
}
}
if (increment == 1) {
first = isFirst.isTrue() ? elements.next() : prev.value();
second = elements.hasNext() ? elements.next() : null;
prev.setValue(second);
} else {
first = elements.next();
second = elements.hasNext() ? elements.next() : null;
}
isFirst.setFalse();
} else {
break;
}
}
action.accept(first, second);
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
try {
complette(futureList, eHolder, (E) null);
} finally {
close();
}
}
@Override
public void forEachTriple(final Try.TriConsumer super T, ? super T, ? super T, E> action, final int increment) throws E {
assertNotClosed();
if (maxThreadNum <= 1) {
super.forEachTriple(action, increment);
return;
}
final int windowSize = 3;
checkArgPositive(increment, "increment");
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
final Holder prev = new Holder<>();
final Holder prev2 = new Holder<>();
final MutableBoolean isFirst = MutableBoolean.of(true);
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Try.Runnable() {
private T first = null;
private T second = null;
private T third = null;
@Override
public void run() {
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
if (increment > windowSize && isFirst.isFalse()) {
int skipNum = increment - windowSize;
while (skipNum-- > 0 && elements.hasNext()) {
elements.next();
}
if (elements.hasNext() == false) {
break;
}
}
if (increment == 1) {
first = isFirst.isTrue() ? elements.next() : prev2.value();
second = isFirst.isTrue() ? (elements.hasNext() ? elements.next() : null) : prev.value();
third = elements.hasNext() ? elements.next() : null;
prev2.setValue(second);
prev.setValue(third);
} else if (increment == 2) {
first = isFirst.isTrue() ? elements.next() : prev.value();
second = elements.hasNext() ? elements.next() : null;
third = elements.hasNext() ? elements.next() : null;
prev.setValue(third);
} else {
first = elements.next();
second = elements.hasNext() ? elements.next() : null;
third = elements.hasNext() ? elements.next() : null;
}
isFirst.setFalse();
} else {
break;
}
}
action.accept(first, second, third);
}
} catch (Exception e) {
setError(eHolder, e);
}
}
}));
}
try {
complette(futureList, eHolder, (E) null);
} finally {
close();
}
}
@Override
A[] toArray(A[] a) {
assertNotClosed();
try {
return elements.toArray(a);
} finally {
close();
}
}
@Override
public > M toMap(final Function super T, ? extends K> keyMapper, final Function super T, ? extends V> valueMapper,
final BinaryOperator mergeFunction, final Supplier extends M> mapFactory) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.toMap(keyMapper, valueMapper, mergeFunction, mapFactory);
}
// return collect(Collectors.toMap(keyMapper, valueMapper, mapFactory));
// final M res = mapFactory.get();
// res.putAll(collect(Collectors.toConcurrentMap(keyMapper, valueMapper, mergeFunction)));
// return res;
final List> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Callable() {
@Override
public M call() {
M map = mapFactory.get();
T next = null;
try {
while (eHolder.value() == null) {
synchronized (elements) {
if (elements.hasNext()) {
next = elements.next();
} else {
break;
}
}
Collectors.merge(map, keyMapper.apply(next), valueMapper.apply(next), mergeFunction);
}
} catch (Exception e) {
setError(eHolder, e);
}
return map;
}
}));
}
if (eHolder.value() != null) {
close();
throw N.toRuntimeException(eHolder.value());
}
M res = null;
try {
for (ContinuableFuture future : futureList) {
if (res == null) {
res = future.get();
} else {
final M m = future.get();
for (Map.Entry entry : m.entrySet()) {
Collectors.merge(res, entry.getKey(), entry.getValue(), mergeFunction);
}
}
}
} catch (InterruptedException | ExecutionException e) {
throw N.toRuntimeException(e);
} finally {
close();
}
return res;
}
@Override
public > M toMap(final Function super T, ? extends K> keyMapper, final Function super T, ? extends V> valueMapper,
final Collector super V, A, D> downstream, final Supplier extends M> mapFactory) {
assertNotClosed();
if (maxThreadNum <= 1) {
return super.toMap(keyMapper, valueMapper, downstream, mapFactory);
}
// return collect(Collectors.groupingBy(keyMapper, downstream, mapFactory));
// final M res = mapFactory.get();
// res.putAll(collect(Collectors.groupingByConcurrent(keyMapper, downstream)));
// return res;
final Supplier downstreamSupplier = downstream.supplier();
final BiConsumer downstreamAccumulator = downstream.accumulator();
final BinaryOperator downstreamCombiner = downstream.combiner();
final Function downstreamFinisher = downstream.finisher();
final List>> futureList = new ArrayList<>(maxThreadNum);
final Holder eHolder = new Holder<>();
for (int i = 0; i < maxThreadNum; i++) {
futureList.add(asyncExecutor.execute(new Callable