SpringBoot项目给定时任务配置线程池

本文介绍了如何在SpringBoot项目中为定时任务配置线程池,以实现多个任务并行执行。当使用@Scheduled注解时,任务默认按单线程顺序执行。为实现并行,可以创建线程池,示例代码展示了如何操作,最终达到任务几乎同时执行的效果。
摘要由CSDN通过智能技术生成

上篇文章简单介绍了怎么在SpringBoot项目中运用简单的定时(间隔)任务,并且举例用的是单个任务,如果是多个定时(间隔)任务,并且想同时执行,应该怎么办?

首先经过了解,@Scheduled是单线程的,如果有多个定时任务,势必需要前一个任务执行完才会执行后面的任务,先放代码:

package com.wl.standard.task;

import com.wl.standard.entity.City;
import com.wl.standard.entity.CityGdp;
import com.wl.standard.entity.CityRail;
import com.wl.standard.service.CityGdpService;
import com.wl.standard.service.CityRailService;
import com.wl.standard.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * 间隔任务
 * @author wl
 * @date 2021/12/23
 */
@Configuration
@Slf4j
@PropertySource("class
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值