SpringBoot 线程池异步调用

本文介绍了如何在SpringBoot应用中配置ThreadPoolTaskExecutor,包括核心线程池大小、最大线程数等参数,并展示了如何使用@Async和@Scheduled注解实现数据定时任务,确保任务在指定线程池中执行。
摘要由CSDN通过智能技术生成

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");
    }

}

  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值