一、背景
在实际开发中,有些业务需要依赖多线程的返回数据,不是单纯的只执行业务逻辑就好。
二、demo
1.线程池工具类
@Slf4j
public class ThreadPoolUtils {
private static ThreadPoolTaskExecutor poolTaskExecutor = null;
private ThreadPoolUtils() {
}
public static ThreadPoolTaskExecutor getInstance() {
try {
if (poolTaskExecutor == null) {
poolTaskExecutor = new ThreadPoolTaskExecutor();
// 设置核心线程数
poolTaskExecutor.setCorePoolSize(10);
// 设置最大线程数
poolTaskExecutor.setMaxPoolSize(100);
// 设置队列容量
poolTaskExecutor.setQueueCapacity(50000);
// 设置线程活跃时间(秒)
poolTaskExecutor.setKeepAliveSeconds(200);
// 设置默认线程名称
poolTaskExecutor.setThreadNamePrefix("taskExecutor-");
// 设置拒绝策略 不在新线程中执行任务,而是有调用者所在的线程来执行
pool