您的当前位置:首页正文

SpringBoot 线程池异步调用

2024-11-20 来源:个人技术集锦

yml配置文件

# 线程池
threadPool:
  # 核心线程池大小
  corePoolSize: 2
  # 最大线程数
  maxPoolSize: 5
  # 缓冲队列大小
  queueCapacity: 200
  # 允许线程空闲时间(单位:默认为秒)
  keepAliveSeconds: 10
  # 允许线程等待时长(单位:默认为秒)
  awaitTerminationSeconds: 60
  # 等待时长(单位:默认为秒)
  threadNamePrefix: Async-Thread-
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Description 线程池配置
 * @Author WangKun
 * @Date 2024/3/5 17:40
 * @Version
 */
@Configuration
public class ThreadPoolConfig {

    /**
     * 获取系统的CPU个数
     */
    private static final int CPU_NUMS = Runtime.getRuntime().availableProcessors();

    /**
     * 核心线程池大小
     */
    @Value("${threadPool.corePoolSize}")
    private int corePoolSize;

    /**
     * 最大线程数
     */
    @Value("${threadPool.maxPoolSize}")
    private int maxPoolSize;

    /**
     * 缓冲队列大小
     */
    @Value("${threadPool.queueCapacity}")
    private int queueCapacity;

    /**
     * 允许线程空闲时间(单位:默认为秒)
     */
    @Value("${threadPool.keepAliveSeconds}")
    private int keepAliveSeconds;

    /**
     * 允许线程等待时长(单位:默认为秒)
     */
    @Value("${threadPool.awaitTerminationSeconds}")
    private int awaitTerminationSeconds;
    /**
     * 线程池名前缀
     */
    @Value("${threadPool.threadNamePrefix}")
    private String threadNamePrefix;

    /**
     * @Description 线程池初始化
      * @param
     * @Throws
     * @Return org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
     * @Date 2024-03-06 17:48:28
     * @Author WangKun
     */
    @Bean(name = "threadPoolExecutor")
    public ThreadPoolTaskExecutor customExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        // 核心线程池大小
        threadPoolTaskExecutor.setCorePoolSize(CPU_NUMS * corePoolSize);
        // 最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(CPU_NUMS * maxPoolSize);
        // 缓冲队列大小
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        // 允许线程空闲时间(单位:默认为秒)
        threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        // 等待时长,默认情况下,执行器不会等待任务的终止,它会立即关闭,中断正在进行的任务和清理,剩余的任务队列
        threadPoolTaskExecutor.setAwaitTerminationSeconds(awaitTerminationSeconds);
        // 线程池名前缀
        threadPoolTaskExecutor.setThreadNamePrefix(threadNamePrefix);
        // CallerRunsPolicy:如果添加到线程池失败,那么主线程会自己去执行该任务,不会等待线程池中的线程去执行。
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //调度器shutdown被调用时等待当前被调度的任务完成
        threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        // 初始化
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }

}

引用:在方法上加注解

@Async("threadPoolExecutor")

例:

/**
 * @Description 数据定时任务
 * @Author WangKun
 * @Date 2024/3/5 17:23
 * @Version
 */
@Slf4j
@Configuration
public class DataTask {
  

    @Async("threadPoolExecutor")
    @Scheduled(cron = "0/5 * * * * *")
    public void cron() {
        log.info("111111");
        System.out.println("111111111111");
        log.info("222222");
    }

}

显示全文