The previous iteration at Parallel for loop in Java 8 - follow-up.
The changes are as follows:
MyTask
is removed.- Synchronization removed. Now the user is assumed not to tamper with the input list concurrently while some
forp
is working on it. - Actual
forp
implemented using Java 8 -facilities.
The only changed file (ParallelFor.java
):
package net.coderodde.util;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* This class implements the parallel for loop. It is assumed that two distinct
* tasks in the loop are independent, i.e., one task needs no output data from
* another task.
*
* @author Rodion Efremov
* @version I
*/
public class ParallelFor {
/**
* Implements the actual parallel for loop. The user is responsible for not
* to modify <code>inputList</code> while some invocation of
* <code>forp</code> is using it.
*
* @param <I> the type of input data.
* @param <O> the type of output data.
* @param inputList the list of individual arguments to the routine to
* parallelize.
* @param body the actual function transforming an input datum into an
* output datum. May be specified in the form of a
* lambda expression.
* @return the list of output data in the same order as input data.
*/
public static final <I, O>
List<O> forp(final List<I> inputList,
final Function<I, O> body) {
return inputList.parallelStream()
.map(body)
.collect(Collectors.toList());
}
}