Java Reference
In-Depth Information
Listing 7.2. Executing a parallel sum using the fork/join framework
Writing a method performing a parallel sum of the first n natural numbers is now pretty
straightforward. You just need to pass the desired array of numbers to the constructor of
ForkJoinSumCalculator:
public static long forkJoinSum(long n) {
long[] numbers = LongStream.rangeClosed(1, n).toArray();
ForkJoinTask<Long> task = new ForkJoinSumCalculator(numbers);
return new ForkJoinPool().invoke(task);
}
 
Search WWH ::




Custom Search