I want to calculate a^b , e.g. 2^30,
public long pow(final int a, final int b)
first I used this manner
return LongStream.range(0, b).reduce(1, (acc, x) -> a * acc); // 1073741824
Got right result. Then I want to calculate it parallelly, 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 just 32. Why?
So for supporting 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's wrong with parallel manner?