I want to calculate a ^ b, for example. 2 ^ 30,
public long pow(final int a, final int b)
I used this method first
return LongStream.range(0, b).reduce(1, (acc, x) -> a * acc); // 1073741824
Got the correct result. Then I want to calculate it in parallel, so naturally I changed it to
return LongStream.range(0, b).parallel().reduce(1, (acc, x) -> a * acc); // 32
but in this case the result is equal 32
. Why?
So, to support the parallel, I changed it again
return Collections.nCopies(b,a).parallelStream().reduce(1, (acc, x) -> acc * x); // 1073741824
in this case it works.
So what happened to the parallel
image?
source
share