Suppose we have a list and want to pick all the elements satisfying a property(let say some functions f). There are 3 ways to parallel this process.
One :
listA.parallelStream.filter(element -> f(element)).collect(Collectors.toList());
Two:
listA.parallelStream.collect(Collectors.partitioningBy(element -> f(element))).get(true);
Three:
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
//separate the listA into several batches
for each batch {
Future<List<T>> result = executorService.submit(() -> {
// test the elements in this batch and return the validate element list
});
}
//merge the results from different threads.
Suppose the testing function is a CPU intensive task. I want to know which method is more efficient. Thanks a lot.