Initialize the thread pool

@Configuration
@EnableAsync
@Slf4j
public class AsyncConfig implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setKeepAliveSeconds(1000);// Free time
        taskExecutor.setCorePoolSize(5);// Thread pool size
        taskExecutor.setMaxPoolSize(5);// Maximum number of threads in the pool
        taskExecutor.setQueueCapacity(0);// Maximum number of waiting tasks
        taskExecutor.setThreadNamePrefix("thread-pool-");/ / prefix
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // Reject the policy
        taskExecutor.initialize();
        return taskExecutor;
    }


    /** * Custom exception handling *@return* /
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new MyAsyncExceptionHandler();
    }

    static class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
        @Override
        public void handleUncaughtException(Throwable throwable, Method method, Object. objects) {
            String methodName = method.getName();
            log.info("Current exception method name,{}", methodName); }}}Copy the code

2. Configuration Description

  • CorePoolSize The minimum number of threads maintained by the thread pool
  • KeepAliveSeconds thread pool maintains the free time allowed by threads
  • MaxPoolSize Maximum number of maintained threads in the thread pool
  • QueueCapacity The buffered queue used by the thread pool

When a task is added to the thread pool using the execute(Runnable) method:

  • If the number of threads in the thread pool is less than corePoolSize at this point, new threads are created to handle the added tasks even if all threads in the thread pool are idle.
  • If the number of threads in the thread pool is equal to corePoolSize, but the buffer queue workQueue is not full, then the task is put into the buffer queue.
  • If the number of threads in the pool is greater than corePoolSize, the buffer workQueue is full, and the number of threads in the pool is less than maximumPoolSize, a new thread is created to handle the added task.
  • If the number of threads in the pool is greater than corePoolSize, the buffer queue workQueue is full, and the number of threads in the pool is equal to maximumPoolSize, the task is processed using the policy specified by the handler. The priority of the rejected task is corePoolSize, workQueue, and maximumPoolSize. If all three are full, the rejected task is processed by handler.
  • When the number of threads in the thread pool is greater than corePoolSize, if a thread is idle for longer than keepAliveTime, the thread is terminated. In this way, the thread pool can dynamically adjust the number of threads in the pool.

There are four default rejection policies:

  • AbortPolicy: the default reject policy. Exceptions thrown directly by ThreadPoolExecutor are not handled.
  • CallerRunsPolicy: After a rejected task is added, the thread of the current thread pool is called to execute the rejected task. (Which thread created the thread pool to execute the rejected task)
  • DiscardPolicy: Using this denial policy, tasks rejected by the thread pool are discarded without throwing exceptions or executing them.
  • DiscardOldestPolicy: When a task is denied, the system discards the oldest task in the task queue and adds a new task to the queue.

Create an asynchronous task class

  1. Create an asynchronous task class
@Slf4j
@Service("asyncService")
public class AsyncService {
    @Async
    public void executorAsyncTask(String name){
        log.info("Perform asynchrony: {}",name); }}Copy the code