一、定义线程池

将创建好的线程池对象放入 taskExecutor 对象中

  1. package com.wells.demo.customer.pool;
  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. * Description
  10. * Created by wells on 2020-07-15 21:18:04
  11. */
  12. @EnableAsync
  13. @Configuration
  14. public class ThreadPoolConfig {
  15. @Bean(value = "taskExecutor")
  16. public Executor createThreadPoolExecutor() {
  17. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  18. executor.setCorePoolSize(10);
  19. executor.setMaxPoolSize(20);
  20. executor.setQueueCapacity(200);
  21. executor.setKeepAliveSeconds(60);
  22. executor.setThreadNamePrefix("taskExecutor-");
  23. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  24. return executor;
  25. }
  26. }

二、定义CustomerAsyncController: 调用入口

  1. package com.wells.demo.customer.controller;
  2. import com.wells.demo.customer.service.CustomerAsyncService;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. /**
  10. * Description 异步调用Service
  11. * Created by wells on 2020-07-15 09:26:38
  12. */
  13. @RestController
  14. @RequestMapping(value = "customer/async")
  15. public class CustomerAsyncController {
  16. Logger logger = LoggerFactory.getLogger(CustomerAsyncController.class);
  17. @Autowired
  18. private CustomerAsyncService customerAsyncService;
  19. @GetMapping(value = "/execute/task")
  20. public String executeTask() throws InterruptedException {
  21. logger.info("customer async request start");
  22. long start = System.currentTimeMillis();
  23. customerAsyncService.executeTaskOne();
  24. customerAsyncService.executeTaskTwo();
  25. long end = System.currentTimeMillis();
  26. logger.info("customer async request end, cost:{}", (end - start));
  27. return "success";
  28. }
  29. }

三、定义Service

  1. package com.wells.demo.customer.service;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.scheduling.annotation.Async;
  5. import org.springframework.stereotype.Service;
  6. import java.util.concurrent.TimeUnit;
  7. /**
  8. * Description
  9. * Created by wells on 2020-07-15 09:36:29
  10. */
  11. @Service
  12. public class CustomerAsyncService {
  13. Logger logger = LoggerFactory.getLogger(CustomerAsyncService.class);
  14. @Async(value = "taskExecutor")
  15. public void executeTaskOne() throws InterruptedException {
  16. logger.info("executeTaskOne request start");
  17. TimeUnit.SECONDS.sleep(10);
  18. logger.info("executeTaskOne request end");
  19. }
  20. @Async(value = "taskExecutor")
  21. public void executeTaskTwo() throws InterruptedException {
  22. logger.info("executeTaskTwo request start");
  23. TimeUnit.SECONDS.sleep(10);
  24. logger.info("executeTaskTwo request end");
  25. }
  26. }

通过 Controller 提供的 url测试,观察日志

四、优雅关闭线程池

setAwaitTerminationSeconds

  1. package com.wells.demo.customer.pool;
  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. * Description
  10. * Created by wells on 2020-07-15 21:18:04
  11. */
  12. @EnableAsync
  13. @Configuration
  14. public class ThreadPoolConfig {
  15. @Bean(value = "taskExecutor")
  16. public Executor createThreadPoolExecutor() {
  17. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  18. executor.setCorePoolSize(10);
  19. executor.setMaxPoolSize(20);
  20. executor.setQueueCapacity(200);
  21. executor.setKeepAliveSeconds(60);
  22. executor.setThreadNamePrefix("taskExecutor-");
  23. // 设置线程池关闭时间
  24. executor.setAwaitTerminationSeconds(60);
  25. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  26. return executor;
  27. }
  28. }

五、代码示例

SpringBoot Async Customer ThreadPool