今天用scheduled写定时任务的时候发现定时任务一秒重复执行一次,而我的cron表达式为 * 0/2 * * * * 。
在源码调试的过程中,发现是我的定时任务执行过程太短导致的。
于是我另外写了个简单的定时任务
@Component
public class TestJob {
@Scheduled(cron = "* 0/2 * * * *")
public void test() {
System.out.println("测试开始");
System.out.println("测试结束");
}
}
上述任务在启动之后一直执行。
然后我在任务后面加入线程睡眠1分钟。
@Component
public class TestJob {
@Scheduled(cron = "* 0/2 * * * *")
public void test() {
System.out.println("测试开始");
System.out.println("测试结束");
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("睡眠结束");
}
}
上述任务执行一次就没有再执行了。
<