Skip to content
Advertisement

How to use executor service in loop for each iteration?

private ExecutorService executorService = Executors.newFixedThreadPool(3);

public void myMethod(){
    int batchSize = 2;
    // calling for 3 pages to test
    for(int i=0;i<3; i++) {
        int[] pageNum = {-1};
        executorService.execute(() -> {
            pageNum[0] = pageNum[0] + 1;
            fetchDataAndPushEvents(pageNum[0], batchSize);  
        }); 
    }
}
private void fetchDataAndPushEvents(int pageNum, int batchSize) {
    log.info("Fetching data for page number: {} and batch size: {}", pageNum, batchSize);
        
    long startTime = System.currentTimeMillis();
    Pageable pageable = PageRequest.of(pageNum, batchSize);
    List<AnswerDto> result = myService.getAllResponse(pageable).getContent();
    long secondTime = System.currentTimeMillis();
    log.info("Time taken to fetch data for page number: {} and batch size: {} is :{}", pageNum, batchSize, secondTime-startTime);

    if(!CollectionUtils.isEmpty(result)) {
        log.info("Pushing events for page number: {} and batch size: {}", pageNum, batchSize);
        pushSomeEvent(result);
        log.info("Time taken to push event for page number: {} and batch size: {} is :{}", pageNum, batchSize, System.currentTimeMillis()-secondTime);
        } else {
            // to terminate the process
            return;
        }
    }

My aim is to fetch data of batch size 2(say) and for 3(say) batches/pages as mentioned in the above code. I’m trying to give the responsibility for fetching the data and pushing the event for a page to a thread i.e. 1 thread for 1 page. I worked on it and able to write the above logic.

But when I’m trying to run the above code, I’m getting the same 2 response(as the batch size) from the getAllResponse repository call for all three iterations. The pageNum is not updating. Getting all responses of page 0, hence the same event is getting pushed in all 3 iterations. How should I approach this problem? PS: I have not much knowledge about executorService.

Advertisement

Answer

You are not increasing the pageNum it will have same value for each iteration. you have to use separate counter variable for it like below.

 int pageNum = 0; 
 for(int i=0, pageCounter=0;i<3; i++,pageCounter++) {
    executorService.execute(() -> {
        fetchDataAndPushEvents(pageNum + pageCounter, batchSize);  
    }); 
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement