I was doing some performance evaluation of Java aggeragate operations to iterate over collections. I was doing an evaluation of performance stream
and parallelStream
. But I found that the output of parallelStream
is wrong most of the times. For example in the following code i got wrong output from parallelStream
more than 80% of the time:
public class ParallelStreamPerformance {
static int totaleven = 0;
public static void main(String[] args) {
List randomList = new ArrayList<>();
Random rnd = new Random();
for(int i = 0 ;i < 1000;i++) {
int r = rnd.nextInt(500000);
randomList.add(r);
}
long s1 = System.currentTimeMillis();
randomList.stream().filter(e -> e%2 ==0).forEach(e -> count());
System.out.println("Even: "+totaleven);
long e1 = System.currentTimeMillis();
System.out.println(e1 - s1);
totaleven = 0;
long s2 = System.currentTimeMillis();
randomList.parallelStream().filter(e -> e%2 ==0).forEach(e -> count());
System.out.println("Even: "+totaleven);
long e2 = System.currentTimeMillis();
System.out.println(e2 - s2);
}
public static void count() {
totaleven++;
}
}
My question is: Am I using parallelStream
in a wrong way? Is there any way to ensure the correctness of parallelStream
.
Thanks
Answer
I think your code having issue with count()
method. As parallelStream
will try to perform task concurrently. This method should be synchronized
or you can make totaleven
as Atomtic Integer. Hope it helps.
No comments:
Post a Comment