线程池配置

痛定思痛。 2023-10-05 23:24 194阅读 0赞
  1. package com.yxoa.qroom.utils;
  2. import lombok.Data;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.scheduling.annotation.AsyncConfigurer;
  6. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  7. import java.util.concurrent.Executor;
  8. import java.util.concurrent.ThreadPoolExecutor;
  9. /**
  10. * @Auther:
  11. * @Date: 2022/8/25 10:02
  12. * @Description:
  13. */
  14. @Configuration
  15. @Data
  16. public class ExecutorConfig implements AsyncConfigurer {
  17. /**
  18. * 核心线程
  19. */
  20. private int corePoolSize=20;
  21. /**
  22. * 最大线程
  23. */
  24. private int maxPoolSize=100;
  25. /**
  26. * 队列容量
  27. */
  28. private int queueCapacity=1000;
  29. /**
  30. * 保持时间
  31. */
  32. private int keepAliveSeconds=300;
  33. /**
  34. * 名称前缀
  35. */
  36. private String preFix="ExecutorPool";
  37. @Bean
  38. public Executor myExecutor() {
  39. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  40. executor.setCorePoolSize(corePoolSize);
  41. executor.setMaxPoolSize(maxPoolSize);
  42. executor.setQueueCapacity(queueCapacity);
  43. executor.setKeepAliveSeconds(keepAliveSeconds);
  44. executor.setThreadNamePrefix(preFix);
  45. executor.setRejectedExecutionHandler( new ThreadPoolExecutor.AbortPolicy());
  46. executor.initialize();
  47. return executor;
  48. }
  49. }

发表评论

表情:
评论列表 (有 0 条评论,194人围观)

还没有评论,来说两句吧...

相关阅读

    相关 线合理配置

    为了合理的配置线程池的线程数,我们把任务分为两种类型,第一种是CPU密集型,第二种是IO密集型。CPU密集型指的是没有IO交互的任务,就是没有用数据库,也没有生成文件,也没有网