Служба Executor не закрывается должным образом при использовании ее с завершением будущего - PullRequest
0 голосов
/ 19 марта 2020

я выполняю три задачи асинхронно

  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)); 

после этого я присоединяюсь ко всем этим задачам

CompletableFuture.allOf(iCommentFuture, iStoryDetailCompletableFuture, iImageFuture)
          ;

это, кажется, работает нормально, но когда я решаю запустить Завершаемое будущее в Исполнителе 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())
      ;

не выполняется корректное отключение службы исполнителя, дальнейший запрос к контроллеру блокируется, что мне здесь не хватает, спасибо за помощь в Advance

после удаления thenRun (() -> executorService.shutdown ()) вроде бы нормально работает, но как правильно отключить Exceutorservice

1 Ответ

0 голосов
/ 19 марта 2020

shutdown () ожидает завершения всех существующих управляемых потоков. Если вам не важно, завершены ли потоки, используйте shutdownNow ().

...