如何利用spring boot实现毫秒级别的定时任务

2024-11-22 07:32:07
推荐回答(1个)
回答1:

private Integer count_first = 1;
private Integer count_second = 1;
private Integer count_three = 1;

@Scheduled(fixedRate = 10000)
public void printCurrentTime() throws InterruptedException {
System.out.println(String.format("① 第%s次执行,当前时间为:%s", count_first++, dateFormat.format(new Date())));
}

@Scheduled(fixedDelay = 10000)
public void printCurrentTimeAfterSleep() throws InterruptedException {
System.out.println(String.format("② 第%s次执行,当前时间为:%s", count_second++, dateFormat.format(new Date())));
}

@Scheduled(cron = "*/10 * * * * *")
public void printCurrentTimeCron() throws InterruptedException {
System.out.println(String.format("③ 第%s次执行,当前时间为:%s", count_three++, dateFormat.format(new Date())));
}

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");