Reputation: 435
im executing three tasks asynchronously
CompletableFuture<IStoryDetail> iStoryDetailCompletableFuture = CompletableFuture.supplyAsync(()->storyRepository.getStoryDetails(id));
CompletableFuture<List<Comment>> iCommentFuture = CompletableFuture.supplyAsync(() -> commentRepository.getComments(id));
CompletableFuture<List<Images>> iImageFuture = CompletableFuture.supplyAsync(() -> imageRepository.getImagesByStoryId(id));
after that im joining all these Tasks
CompletableFuture.allOf(iCommentFuture, iStoryDetailCompletableFuture, iImageFuture)
;
this seems to work fine but when i decide to run the Completable future in a Executor service
@Autowired
ICommentRepository commentRepository;
ExecutorService executorService = Executors.newFixedThreadPool(4);
CompletableFuture<IStoryDetail> iStoryDetailCompletableFuture = CompletableFuture.supplyAsync(()->storyRepository.getStoryDetails(id),executorService);
CompletableFuture<List<Comment>> iCommentFuture = CompletableFuture.supplyAsync(() -> commentRepository.getComments(id),executorService);
CompletableFuture<List<Images>> iImageFuture = CompletableFuture.supplyAsync(() -> imageRepository.getImagesByStoryId(id),executorService);
iStoryDetailCompletableFuture.thenApply(story -> objectMap.putIfAbsent(story.getClass().getName(),story));
iCommentFuture.thenApply(comments -> objectMap.putIfAbsent(Comment.class.getName(),comments));
iImageFuture.thenApply(images -> objectMap.putIfAbsent(Images.class.getName(),images));
CompletableFuture.allOf(iCommentFuture, iStoryDetailCompletableFuture, iImageFuture).thenRun(() -> executorService.shutdown())
;
the executor service doesn't seems to shutdown correctly , further request to the controller is getting blocked, what am i missing here thanks for the help in Advance
after removing thenRun(() -> executorService.shutdown()) it seems to work fine , but how to shut down Exceutorservice properly
Upvotes: 0
Views: 448
Reputation: 1760
shutdown() waits for any existing managed threads to complete. If you don't care if threads it's managing complete, then use shutdownNow().
Upvotes: -2