spring-boot a custom thread pool

  • Define a thread pool Spring Boot main class, such as:
@SpringBootApplication
public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @EnableAsync @Configuration class TaskPoolConfig { @Bean("taskExecutor") public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(10); executor.setMaxPoolSize(20); executor.setQueueCapacity(200); executor.setKeepAliveSeconds(60); executor.setThreadNamePrefix("taskExecutor-"); executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); executor.setWaitForTasksToCompleteOnShutdown(true); executor.setAwaitTerminationSeconds(60); return executor; } } } 

By the above we ThreadPoolTaskExecutorcreate a thread pool, and set the following parameters:

  • 10 Number of kernel threads: Number of threads created to initialize the thread pool
  • 20 maximum number of threads: The maximum number of threads in the thread pool, only after the buffer queue is full it will apply more than the number of threads core threads
  • Buffer queue 200: to perform the task queue buffer
  • Allowing the thread idle time of 60 seconds: more than the number of threads outside the core thread, it will be destroyed after the idle time of arrival
  • Prefix name of the thread pool: we can easily locate the thread pool where after processing tasks set up
  • Thread pool strategy rejected tasks: here uses a CallerRunsPolicystrategy when the thread pool is not processing power, the policy will directly executerun the task rejected the calling thread method; unless the executor has been shut down, it will be discarded the task
  • Set the thread pool is closed while waiting for the completion of all tasks to continue the destruction of other Bean
  • Set the thread pool task of waiting time, if more than this time not to force the destruction of the destruction, in order to ensure that the application can finally be closed, instead of blocking live
Use the thread pool
  • Only you need to @Asyncspecify the thread pool name to annotations
@Slf4j
@Component
public class Task { public static Random random = new Random(); @Async("taskExecutor") public void doTaskOne() throws Exception { log.info("开始做任务一"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); log.info("完成任务一,耗时:" + (end - start) + "毫秒"); } @Async("taskExecutor") public void doTaskTwo() throws Exception { log.info("开始做任务二"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); log.info("完成任务二,耗时:" + (end - start) + "毫秒"); } @Async("taskExecutor") public void doTaskThree() throws Exception { log.info("开始做任务三"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); log.info("完成任务三,耗时:" + (end - start) + "毫秒"); } } 
  • test
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest public class ApplicationTests { @Autowired private Task task; @Test public void test() throws Exception { task.doTaskOne(); task.doTaskTwo(); task.doTaskThree(); Thread.currentThread().join(); } }


Author: Dear_diary
link: https: //www.jianshu.com/p/a13ac0d774c6
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/zeenzhou/p/12664154.html