赞
踩
目录
在spring中,我们可以用@Scheduled来做定时器, 它支持cron,固定周期定时,其使用方法如下
- @Component // ①
- @EnableScheduling //②
- public class TestSchedule {
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- @Scheduled(fixedDelay = 3000) //③
- private void testTask(){
- System.out.println("定时任务执行时间:" + LocalDateTime.now().format(dateTimeFormatter));
- }
- }
以上代码注释
①是该类被容器自动管理
② @EnableScheduling注解使我们的scheduled定时器生效
③ @Scheduled(fixedDelay = 3000) 使用了该注解的方法,按指定的规则时间执行
fixedDelay 固定延时,什么意思,fixedDelay值表示本次方法执行完成下次执行需要等待的时间,按照下面的代码,如果我们方法执行需要2000毫秒,那么我们方法下次执行时间是3000+2000=5000毫秒,代码如下
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- @Scheduled(fixedDelay = 3000)
- private void testTask() throws InterruptedException {
- Thread.sleep(2000);
- System.out.println("定时任务执行时间:" + LocalDateTime.now().format(dateTimeFormatter));
- }
执行结果
定时任务执行时间:2021-12-01 20:37:42
定时任务执行时间:2021-12-01 20:37:47
定时任务执行时间:2021-12-01 20:37:52
定时任务执行时间:2021-12-01 20:37:57
定时任务执行时间:2021-12-01 20:38:02
定时任务执行时间:2021-12-01 20:38:07
定时任务执行时间:2021-12-01 20:38:12
定时任务执行时间:2021-12-01 20:38:17
其中可以看出,我们方法执行周期等于fixedDelay+方法本身执行时间
fixedRate 这个参数作用是什么呢,如下代码
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- @Scheduled(fixedRate = 3000)
- private void testTask() throws InterruptedException {
- Thread.sleep(2000);
- System.out.println("定时任务执行时间:" + LocalDateTime.now().format(dateTimeFormatter));
- }
执行结果是:
定时任务执行时间:2021-12-01 14:48:22
定时任务执行时间:2021-12-01 14:48:25
定时任务执行时间:2021-12-01 14:48:28
定时任务执行时间:2021-12-01 14:48:31
定时任务执行时间:2021-12-01 14:48:34
从执行结果,可以看出,如果使用了fixedRate,如果方法本身执行时间小于fixedRate,那么方法下次执行时间就是 fixedRate设置的值
如果方法中的执行时间大于fixedRate呢?
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- @Scheduled(fixedRate = 3000)
- private void testTask() throws InterruptedException {
- Thread.sleep(5000);
- System.out.println("定时任务执行时间:" + LocalDateTime.now().format(dateTimeFormatter));
- }
执行结果
定时任务执行时间:2021-12-01 14:51:16
定时任务执行时间:2021-12-01 14:51:21
定时任务执行时间:2021-12-01 14:51:26
那么方法的执行周期就是按照方法执行时间
我们的定时器也可以按照cron方法来执行
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- @Scheduled(cron = "*/3 * * * * *")
- private void testTask() throws InterruptedException {
- System.out.println("定时任务执行时间:" + LocalDateTime.now().format(dateTimeFormatter));
- }
每隔三秒执行1次
如果我们方法执行也需要一定时间,会是怎样情况呢
如果方法体执行时间小于cron设置的周期,就按照cron设置周期执行
如果方法体执行时间大于cron设置周期,那么方法体执行周期就大于cron设置的周期,大家可以做实验
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- ScheduledExecutorService executorService=new ScheduledThreadPoolExecutor(1);
- executorService.scheduleAtFixedRate(()->{
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();e.printStackTrace();
- }
- System.out.println(dtf.format(LocalDateTime.now())+"dd");
- },1,3, TimeUnit.SECONDS);
ScheduledExecutorService 里面有 scheduleAtFixedRate 和scheduleWithFixedDelay用法和我们上面所提定时器的fixedRate、fixedDelay的用法差不多.
我们发现ScheduledExecutorService没有cron的相关调用方法
1 ScheduledExecutorService 怎样才能实现cron表达式调用
2 @Scheduled定时 和 ScheduledExecutorService定时有什么关系
3 为什么要加上@EnableScheduling ,@Scheduled才生效
4 @Scheduled在什么地方、什么时候解析的
5 定时器原理有哪些,ScheduledExecutorService定时器的原理是什么
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。