Ashishkumar Singh
Ashishkumar Singh

Reputation: 3600

Suggestion : Making parallel call from my service to another micro service

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

  1. Is using ExecutorService the best approach for my scenario. Or I should be using something else
  2. Should I create a new executor like Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()) or let container inject a managed Executor using @Autowired

Upvotes: 0

Views: 594

Answers (1)

htshame
htshame

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

Related Questions