AsyncConfig.java 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package com.yihu.wlyy.statistics.config;
  2. import java.util.concurrent.Executor;
  3. import org.apache.tomcat.util.threads.ThreadPoolExecutor;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.scheduling.annotation.EnableAsync;
  7. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  8. /**
  9. * Created by Administrator on 2016.10.18.
  10. * 启用多綫程
  11. */
  12. @Configuration
  13. @EnableAsync
  14. public class AsyncConfig {
  15. /** 如果池中的实际线程数小于corePoolSize,无论是否其中有空闲的线程,都会给新的任务产生新的线程 */
  16. private int corePoolSize = 5;
  17. /** 如果池中的线程数=maximumPoolSize,则有空闲线程使用空闲线程,否则新任务放入queueCapacity. */
  18. private int maxPoolSize = 20;
  19. /** 缓冲队列. */
  20. private int queueCapacity = 10;
  21. @Bean
  22. public Executor dbExtractExecutor() {
  23. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  24. executor.setCorePoolSize(corePoolSize);
  25. executor.setMaxPoolSize(maxPoolSize);
  26. executor.setQueueCapacity(queueCapacity);
  27. // rejection-policy:当pool已经达到max size的时候,如何处理新任务
  28. // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
  29. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  30. executor.initialize();
  31. return executor;
  32. }
  33. @Bean
  34. public Executor dbStorageExecutor() {
  35. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  36. executor.setCorePoolSize(corePoolSize);
  37. executor.setMaxPoolSize(maxPoolSize);
  38. executor.setQueueCapacity(queueCapacity);
  39. // rejection-policy:当pool已经达到max size的时候,如何处理新任务
  40. // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
  41. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  42. executor.initialize();
  43. return executor;
  44. }
  45. }