Reputation: 3600
I have a requirement as follows
I want to make the second step parallel. Now I know based on data that no more than 15 records will be present in list. So I thought of using executorservice for this.
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
Now I am not very confident as to whether
ExecutorService
the best approach for my scenario. Or I should be using something else Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())
or let container inject a managed Executor using @Autowired
Upvotes: 0
Views: 594
Reputation: 7330
Since you have a List
of records, you may iterate through it using parallelStream
.
List<SomeObject> objectList = resultFromDB();
objectList.parallelStream()
.map(obj -> makeApiCallInParallel(obj))
.collect(Collectors.toList());
parallelStream
will perform API calls in parallel. The number of parallel calls is limited by the number of threads of your CPU.
You may increase it using JVM property:
-Djava.util.concurrent.ForkJoinPool.common.parallelism=20
Upvotes: 1