TaskExecutePool.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.tmzn.devicelinkykc.taskQueue.queue;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.scheduling.annotation.EnableAsync;
  5. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  6. import java.util.concurrent.Executor;
  7. import java.util.concurrent.ThreadPoolExecutor;
  8. /**
  9. * 自定义创建线程池
  10. */
  11. @Configuration
  12. @EnableAsync
  13. public class TaskExecutePool {
  14. @Bean
  15. public Executor heartTaskAsyncPool() {
  16. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  17. executor.setCorePoolSize(4); //核心线程数
  18. executor.setMaxPoolSize(8); //最大线程数
  19. executor.setQueueCapacity(1000); //队列大小
  20. executor.setKeepAliveSeconds(300); //线程最大空闲时间
  21. executor.setThreadNamePrefix("async-heartExecutor-");
  22. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略(一共四种,此处省略)
  23. executor.initialize();
  24. return executor;
  25. }
  26. @Bean
  27. public Executor charngingTaskAsyncPool() {
  28. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  29. executor.setCorePoolSize(4); //核心线程数
  30. executor.setMaxPoolSize(8); //最大线程数
  31. executor.setQueueCapacity(1000); //队列大小
  32. executor.setKeepAliveSeconds(300); //线程最大空闲时间
  33. executor.setThreadNamePrefix("async-charngingExecutor-");
  34. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略(一共四种,此处省略)
  35. executor.initialize();
  36. return executor;
  37. }
  38. @Bean
  39. public Executor freeTaskAsyncPool() {
  40. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  41. executor.setCorePoolSize(4); //核心线程数
  42. executor.setMaxPoolSize(8); //最大线程数
  43. executor.setQueueCapacity(1000); //队列大小
  44. executor.setKeepAliveSeconds(300); //线程最大空闲时间
  45. executor.setThreadNamePrefix("async-freeExecutor-");
  46. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略(一共四种,此处省略)
  47. executor.initialize();
  48. return executor;
  49. }
  50. @Bean
  51. public Executor transactionTaskAsyncPool() {
  52. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  53. executor.setCorePoolSize(4); //核心线程数
  54. executor.setMaxPoolSize(8); //最大线程数
  55. executor.setQueueCapacity(1000); //队列大小
  56. executor.setKeepAliveSeconds(300); //线程最大空闲时间
  57. executor.setThreadNamePrefix("async-transactionExecutor-");
  58. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略(一共四种,此处省略)
  59. executor.initialize();
  60. return executor;
  61. }
  62. }