定义线程池

  1. /**
  2. * 自定义线程池,推荐使用
  3. *
  4. * @return XyThreadPoolTaskExecutor
  5. */
  6. @Bean("threadPoolTaskExecutor")
  7. public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
  8. //一定要这一行
  9. XyThreadPoolConfig config = XyThreadPoolConfig.init(this.threadPoolConfig);
  10. RejectedExecutionHandler rejectedExecutionHandler = new CallerRunsPolicy();
  11. if (threadPoolConfig.getHandlerEnum() != null) {
  12. String handlerName = threadPoolConfig.getHandlerEnum().name();
  13. if ("DISCARD_POLICY".equals(handlerName)) {
  14. rejectedExecutionHandler = new DiscardPolicy();
  15. } else if ("ABORT_POLICY".equals(handlerName)) {
  16. rejectedExecutionHandler = new AbortPolicy();
  17. } else if ("DISCARD_OLDEST_POLICY".equals(handlerName)) {
  18. rejectedExecutionHandler = new DiscardOldestPolicy();
  19. }
  20. }
  21. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  22. executor.setCorePoolSize(config.getCoreSize());
  23. executor.setMaxPoolSize(config.getMaxSize());
  24. executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
  25. executor.setQueueCapacity(config.getQueueCapacity());
  26. executor.setAllowCoreThreadTimeOut(true);
  27. //executor.setTaskDecorator();
  28. //executor.setThreadFactory();
  29. executor.setThreadNamePrefix(config.getThreadNamePrefix());
  30. executor.setRejectedExecutionHandler(rejectedExecutionHandler);
  31. //表明等待所有线程执行完
  32. executor.setWaitForTasksToCompleteOnShutdown(true);
  33. //等待xx秒后强制停止
  34. executor.setAwaitTerminationSeconds(10);
  35. //executor.setAwaitTerminationMillis();
  36. executor.setBeanName("xyThreadPoolTaskExecutor");
  37. executor.setThreadPriority(config.getThreadPriority());
  38. executor.setDaemon(false);
  39. executor.setThreadGroupName(config.getThreadGroupName());
  40. executor.afterPropertiesSet();
  41. return executor;
  42. }

配置线程池

在CommonConfig定义了默认的线程池,如需修改线程池的配置,在配置文件中添加相应配置即可。

  1. xy.core.thread.pool.config.core-size=2
  2. xy.core.thread.pool.config.max-size=20
  3. xy.core.thread.pool.config.queue-capacity=200
  4. xy.core.thread.pool.config.thread-name-prefix=sample
  5. xy.core.thread.pool.config.thread-priority=5
  6. xy.core.thread.pool.config.allow-core-thread-time-out=true
  7. xy.core.thread.pool.config.keep-alive-seconds=3000
  8. xy.core.thread.pool.config.thread-group-name=group1
  9. xy.core.thread.pool.config.thread-pool-name=async-pool
  10. xy.core.thread.pool.config.handler-enum=DISCARD_POLICY

若没有显示声明配置,则会提供默认的配置

  1. tpc.setCoreSize(2 * Runtime.getRuntime().availableProcessors() + 1);
  2. tpc.setMaxSize(2 * Runtime.getRuntime().availableProcessors() + 1);
  3. tpc.setThreadNamePrefix(xyThreadPoolConfig.getThreadPoolName() + "-xyThread-");
  4. tpc.setKeepAliveSeconds(100);
  5. tpc.setQueueCapacity(200);
  6. tpc.setThreadGroupName(XyEnvironmentUtils.getApplicationName());
  7. tpc.setThreadPriority(5);

使用线程池

线程池通常做异步方法执行使用,生产的应用框架中也自带了样例。

  1. @Slf4j
  2. @Service
  3. public class AsyncServiceImpl implements AsyncService {
  4. @Override
  5. @Async("sampleThreadPool")
  6. public void test(String name) {
  7. try {
  8. TimeUnit.SECONDS.sleep(10);
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();
  11. }
  12. String message = MessageFormat.format("threadId-:{0},threadName-:{1},call:{2}", Thread.currentThread().getId(), Thread.currentThread().getName(), name);
  13. log.error(message);
  14. }
  15. }