package com.programme;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling//开启定时任务
public class SpringbootJpaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootJpaApplication.class, args);
}
}
package com.programme;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* @program: spring
* @description: ${description}
* @author: Mr.zw
* @create: 2021-05-12 22:50
**/
@Component
public class TaskJob {
/**
* 定时任务方法corn -,/ 日 周 ?
* W:离当前日期最近的工作日 离当前星期几最近的工作日
* L:当月的最后一天 本周的最后一天
*/
//initialDelay:项目启动多久后执行第一次
@Scheduled(initialDelay = 5000,fixedRate = 5000)//上一次执行开始多久后执行下一次
public void task() throws Exception{
System.out.println(System.currentTimeMillis());
Thread.sleep(2000);
}
}

原文:https://www.cnblogs.com/demowei/p/14762423.html