应该在一个 SpringBoot 应用程序中使用单个 ThreadPoolTask​​Executor 还是多个 ThreadPoolTask​​Executor?

时间:2021-06-22 17:47:31

标签: java multithreading spring-boot performance

我有一个 spring boot 应用程序,其中有两个我想要多任务处理的任务,我创建了两个 ThreadPoolTask​​Executor,每个任务一个,这是最好的方法吗?我只需要一个 ThreadPoolTask​​Executor 吗?以下是我的方法,我想知道哪种方法最好。

方法#1

@Bean("billTaskExecutor")
public ThreadPoolTaskExecutor billTaskExecutor() {
  ThreadPoolTaskExecutor billTaskExecutor= new ThreadPoolTaskExecutor();
   billTaskExecutor.setCorePoolSize(6);
   billTaskExecutor.setMaxPoolSize(15);
   billTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
   billTaskExecutor.initialize();
   return billTaskExecutor;
} 

@Bean("rewardsTaskExecutor")
public ThreadPoolTaskExecutor rewardTaskExecutor() {
  ThreadPoolTaskExecutor rewardsTaskExecutor= new ThreadPoolTaskExecutor();
   rewardsTaskExecutor.setCorePoolSize(5);
   rewardsTaskExecutor.setMaxPoolSize(10);
   rewardsTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
   rewardsTaskExecutor.initialize();
   return billTaskExecutor;
} 

@Async("billTaskExecutor")
public CompletableFuture<Bill> getClientBills(String clientName) {
  return CompletableFuture.completedFuture(billProxy.getClientBills(clientName));
}
@Async("rewardsTaskExecutor")
public CompletableFuture<Reward> getClientRewards(String clientName) {
  return CompletableFuture.completedFuture(RewardProxy.getClientBills(clientName));
}

方法#2

@Bean("customTaskExecutor")
    public ThreadPoolTaskExecutor customTaskExecutor() {
      ThreadPoolTaskExecutor customTaskExecutor= new ThreadPoolTaskExecutor();
       customTaskExecutor.setCorePoolSize(5);
       customTaskExecutor.setMaxPoolSize(10);
       customTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
       customTaskExecutor.initialize();
       return customTaskExecutor;
    } 
    
    @Async("customTaskExecutor")
    public CompletableFuture<Bill> getClientBills(String clientName) {
      return CompletableFuture.completedFuture(billProxy.getClientBills(clientName));
    }
    @Async("customTaskExecutor")
    public CompletableFuture<Reward> getClientRewards(String clientName) {
      return CompletableFuture.completedFuture(RewardProxy.getClientBills(clientName));
    }

预先感谢您的建议。

0 个答案:

没有答案