Springboot多线程定时任务

  • 第一需要配置启动类注解开启定时任务功能(开启才可以使用)
@EnableScheduling
public class Application {...}
  • 第二新建类AsyncConfig,代码如下
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * @ClassName:AsyncConfig
 * @Description: * @Configuration:表明该类是一个配置类
 * * @EnableAsync:开启异步事件的支持
 * 在定时任务的类或者方法上添加@Async 。
 * 最后重启项目,每一个任务都是在不同的线程中
 * //------------------------------------------
 * 项目中使用方法:
 * 1.启动类中添加 @EnableScheduling 注解开启定时
 * 2.业务类中添加 @Async(也可在方法中添加)  支持多线程
 * @Component 注解
 * 3.定时处理方法中添加 @Scheduled 使用 cron 表达式 如 每天一点十分执行:"0 10 1 * * *"
 * 秒         0~59               , - * /
 * 分         0~59               , - * /
 * 小时         0~23               , - * /
 * 日期         1-31               , - * ? / L W C
 * 月份         1~12或者JAN~DEC         , - * /
 * 星期         1~7或者SUN~SAT         , - * ? / L C #
 * 年(可选)   留空,1970~2099          , - * /
 * @Author:lids
 * @CreateDate: 2018/6/5 9:58
 */
@Configuration
@EnableAsync
public class AsyncConfig {
    /**
     * 此处成员变量应该使用@Value从配置中读取
     * corePoolSize:如果池中的
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值