定时任务和异步任务测试

package com.kun.seckill.scheduled;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author zhoukun 86547462@qq.com
 * @version 1.0
 * @date 2021/1/10 13:15
 * @EnableScheduling//开启定时任务
 */

/**
 * 定时任务:
 *  @EnableScheduling//开启定时任务
 *  @Scheduled//开启一个定时任务
 *  配置类:TaskSchedulingAutoConfiguration
 * 异步任务:
 *  @EnableAsync//开启异步任务功能
 *  @Async给需要异步的方法上添加注解
 *  配置类:TaskExecutionAutoConfiguration
 *  spring.task.execution.pool.core-size: 5
 *  spring.task.execution.pool.max-size: 50
 */
@Component
@EnableScheduling//开启定时任务
@Slf4j
@EnableAsync
public class HelloScheduled {

    /**
     * cron表达式
     * https://cron.qqe2.com/
     * con 从0秒开始,每2秒执行一次
     * 注意:spring中6位,不存在年
     * 定时任务不应该阻塞,默认是阻塞的,解决方法
     * 1,可以让方法异步执行,自己提交到自己的线程池
     * 2.支持定时任务线程池,配置
     * spring.task.scheduling.pool.size: 5
     * 3.让定时任务异步执行
     *  @EnableAsync//开启异步任务功能
     *  @Async给需要异步的方法上添加注解
     */
    @Async
    @Scheduled(cron = "0/2 * * * * ? ")
    public void helloSchd()
    {
        log.info("======>hello");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值