Springboot thread pool configuration management

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/a787373009/article/details/101625265

Springboot thread pool configuration management

1. thread pool configuration class

package com.bin.kong.csdnspider.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync
public class ThreadPoolConfig {
    private int corePoolSize = 10;//线程池维护线程的最少数量
    private int maxPoolSize = 50;//线程池维护线程的最大数量
    private int queueCapacity = 20; //缓存队列
    private int keepAlive = 120;//允许的空闲时间
    @Bean
    public AsyncTaskExecutor threadExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("threadExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略
        executor.setKeepAliveSeconds(keepAlive);
        executor.initialize();
        return executor;
    }
}

Configuration instructions:

Configuration Item Explanation
corePoolSize The minimum number of threads, the default: 1
maxPoolSize The maximum number of threads, the default: Integer.MAX_VALUE
queueCapacity When the minimum number of threads in the thread pool is occupied full, new tasks which will be put into the queue queue, the queue when the capacity is also filled, pool which will create a new thread to deal with this task until the total number of threads to achieve the maximum number of threads maxsize, then the system will reject the task and throw TaskRejectedException an exception (in the case of default configuration, you can decide how to handle this situation by rejection-policy). The default value is: Integer.MAX_VALUE
keepAlive CorePoolSize those threads exceed the minimum number of threads, the task is completed, and then after this set length of time (unit: seconds) will be the end of the fall, so the thread pool can dynamically adjust the number of threads in the pool
rejection-policy ABORT (default): TaskRejectedException throws an exception, then do not execute; DISCARD: not performed, no exception is thrown to renounce the thread; DISCARD_OLDEST: discarding the oldest queue that task; CALLER_RUNS: not a new thread to perform tasks, but performed by a thread where the caller (not asynchronous)

2. Use the thread pool

In the class method names used: effect of using the thread pool @Async ( "threadExecutor") can be achieved by
the following example:

package com.bin.kong.csdnspider.utils;
import org.springframework.scheduling.annotation.Async;
@Service
public class TestThreadPool {
    @Async("threadExecutor")
    public void exec() {
        System.out.println("当前线程名:"+Thread.currentThread().getName());
    }
}

Print test results are as follows:

The current thread name: threadExecutor-1

Guess you like

Origin blog.csdn.net/a787373009/article/details/101625265