skip/limit in parallel context
Mallwitz, Christian
christian.mallwitz at Commerzbank.com
Thu Dec 13 03:30:08 PST 2012
Hi,
Using jdk1.8.0-b68-09_dec_2012 and having (this is counting even numbers up to 100 in various ways)
import java.util.Arrays;
import java.util.stream.Stream;
public class LambdaParallelLimitSkip {
private static final int n = 100;
public static void main(String[] args) {
testSerial();
testParallel();
}
public static void testSerial() {
System.out.println("serial count after limit(10): " + getArrayBasedStream(false)
.filter(l -> l%2==0) // filter even numbers
.limit(10)
.reduce(0L, (left, right) -> left + 1L)); // count
System.out.println("serial count after skip(10) : " + getArrayBasedStream(false)
.filter(l -> l%2==0) // filter even numbers
.skip(10)
.reduce(0L, (left, right) -> left + 1L)); // count
}
public static void testParallel() {
System.out.println("parallel count after limit(10): " + getArrayBasedStream(true)
.filter(l -> l%2==0) // filter even numbers
.limit(10)
.reduce(0L, (left, right) -> left + 1L)); // count
System.out.println("parallel count after skip(10) : " + getArrayBasedStream(true)
.filter(l -> l%2==0) // filter even numbers
.skip(10)
.reduce(0L, (left, right) -> left + 1L)); // count
}
public static Stream<Long> getArrayBasedStream(boolean parallel) {
Long[] list = new Long[n];
for(int i=0; i<n; i++) { list[i] = i+1L; }
return parallel ? Arrays.asList(list).parallel() : Arrays.asList(list).stream();
}
}
This outputs
serial count after limit(10): 10
serial count after skip(10) : 40
parallel count after limit(10): 7
parallel count after skip(10) : 25
The last item "parallel count after skip(10) : 25" is incorrect - this needs to be in the 40..50 range, doesn't it?
Now I understand that the Java Doc is saying skip/limit 'at most n elements' but IMHO this is not particular useful.
skip/limit _could_ consume more than n elements of a source stream (and calling the filter predicate in my example more often in the parallel than serial case) but should do "exactly n" when passing elements to further steps in the processing downstreams.
Regards
Christian
More information about the lambda-dev
mailing list