Home > Mobile >  Java how to call a method in parallel passing chunked data
Java how to call a method in parallel passing chunked data

Time:03-03

I have an API call that can only accept 5 subrequests at a time. So I need to chunkify if more than 10 subrequests come in and then send them in parallel as per the SLA.

Below is a nonparallel chunkified code and I am looking to parallelize this call invokeService(bulkRequestChunk)

 int BULK_SUBREQUEST_SIZE = 5;
 // infoRequestList size = 7
 for (int i = 0; i < numOfSubRequests; i  = BULK_SUBREQUEST_SIZE) {
                List<InfoRequest> infoRequestChunk;
                if (i   BULK_SUBREQUEST_SIZE >= numOfSubRequests) {
                    infoRequestChunk = infoRequestList.subList(i, numOfSubRequests);
                } else {
                    infoRequestChunk = infoRequestList.subList(i, i   BULK_SUBREQUEST_SIZE);
                }
                BulkRequest bulkRequestChunk = new BulkRequest();
                bulkRequestChunk.setRequests(infoRequestChunk);
                BulkResponse bulkResponseChunk = invokeService(bulkRequestChunk);

                // output list to capture all chunked requests
                bulkResponseList.add(bulkResponseChunk); 
            }

CodePudding user response:

Use an ExecutorService and submit it tasks to run in parallel:

ExecutorService execServ = Executors.newFixedThreadPool(numThreads);
final List<BulkResponse> bulkResponseList = new ArrayList<>();
int BULK_SUBREQUEST_SIZE = 5;

for (int i = 0; i < numOfSubRequests; i  = BULK_SUBREQUEST_SIZE) {
    List<InfoRequest> infoRequestChunk;
    if (i   BULK_SUBREQUEST_SIZE >= numOfSubRequests) {
        infoRequestChunk = infoRequestList.subList(i, numOfSubRequests);
    } else {
        infoRequestChunk = infoRequestList.subList(i, i   BULK_SUBREQUEST_SIZE);
    }
    BulkRequest bulkRequestChunk = new BulkRequest();
    bulkRequestChunk.setRequests(infoRequestChunk);
    execServ.submit(() -> {
        BulkResponse bulkResponseChunk = invokeService(bulkRequestChunk);
        bulkResponseList.add(bulkResponseChunk);
    });
}
execServ.shutdown();
try {
    execServ.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);
} catch (InterruptedException e) {
    
}
  • Related