自定义异步任务线程池

@Async 注解 方法将使用该线程池

  1. @Bean
  2. public Executor taskExecutor() {
  3. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  4. executor.setCorePoolSize(25);
  5. executor.setMaxPoolSize(50);
  6. executor.setQueueCapacity(1000);
  7. executor.setKeepAliveSeconds(60);
  8. executor.setThreadNamePrefix("ASYNC-TASK-THREAD-");
  9. executor.setWaitForTasksToCompleteOnShutdown(true);
  10. executor.setRejectedExecutionHandler(
  11. new ThreadPoolExecutor.CallerRunsPolicy());
  12. executor.initialize();
  13. return executor;
  14. }

自定义调度任务线程池

@Scheduled 注解方法将使用该线程池

  1. @Configuration
  2. public class SchedulerConfig implements SchedulingConfigurer {
  3. @Override
  4. public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
  5. taskRegistrar.setScheduler(getSchedulerExecutor());
  6. }
  7. public Executor getSchedulerExecutor() {
  8. ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(25,
  9. new ThreadFactoryBuilder().setNameFormat("BUSI-SCHEDULER-%d").build());
  10. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  11. return executor;
  12. }
  13. }