AsyncConfig.java 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. package com.cn.tianji.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.scheduling.annotation.AsyncConfigurer;
  5. import org.springframework.scheduling.annotation.EnableAsync;
  6. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  7. import java.util.concurrent.Executor;
  8. import java.util.concurrent.ThreadPoolExecutor;
  9. @Configuration
  10. @EnableAsync
  11. public class AsyncConfig implements AsyncConfigurer {
  12. @Bean(name = "workingConditionCalculationTaskExecutor")
  13. public Executor taskExecutor() {
  14. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  15. executor.setCorePoolSize(10); // 设置核心线程数
  16. executor.setMaxPoolSize(20); // 设置最大线程数
  17. executor.setQueueCapacity(500); // 设置队列容量
  18. executor.setKeepAliveSeconds(60); // 线程空闲时间
  19. executor.setThreadNamePrefix("WorkingConditionCalculationThread-"); // 设置线程名前缀
  20. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy()); // 拒绝策略
  21. executor.initialize();
  22. return executor;
  23. }
  24. @Override
  25. public Executor getAsyncExecutor() {
  26. return taskExecutor();
  27. }
  28. }