【填坑向】记一次使用ScheduledExecutorService.scheduleAtFixedRate()的坑

背景

在使用scheduledExecutorService.scheduleAtFixedRate()时发现定时任务不知道在什么时候就停了,因为有了解到scheduledExecutorService会因为出现异常而停止定时任务,所以我特地加了try/catch捕获异常,代码大致如下:

scheduledExecutorService.scheduleAtFixedRate(() -> {
    try {
        service.backupData(1);
    } catch (Exception e) {
        System.out.println("This is Exception!");
        log.error(e.getMessage());
        System.out.println("=========================");
    }, 1, 10, TimeUnit.SECONDS);

好奇怪= =于是又去仔细翻阅了官方文档里面的记录

官网说明

Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given period; that is executions will commence after initialDelay then initialDelay+period, then initialDelay + 2 * period, and so on. If any execution of the task encounters an exception, subsequent executions are suppressed. Otherwise, the task will only terminate via cancellation or termination of the executor. If any execution of this task takes longer than its period, then subsequent executions may start late, but will not concurrently execute.

翻并分析下

创建并执行一个周期性的动作,该动作在给定的初始延迟(initialDelay)后,会先执行一下,随后在给定的周期内不断循环执行;也就是说,执行将在initialDelay之后开始第一次执行,然后是 initialDelay+period,然后是 initialDelay + 2 * period,依此类推。 如果任务的任何执行遇到异常,则后续执行将被抑制。 否则,任务只会通过取消或终止执行者来终止。 如果此任务的任何执行时间超过其周期,则后续执行可能会延迟开始,但不会同时执行。

好像官网提供的只有报异常这一种情况,定时任务会终止,可是我已经catch Exception叻

到底为啥,,知道我查日志的时候忽然发现Java可不只有Exception,他还有Error!!

由于Exception和Error都继承了Throwable,所以我改掉了Exception,换成了Throwable

参考:

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ScheduledExecutorService.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ScheduledExecutorService.scheduleAtFixedRate是Java中的一个方法,它用于在给定的初始延迟后开始执行任务,并且在指定的时间间隔内重复执行任务。 在使用scheduledExecutorService.scheduleAtFixedRate()时,如果出现异常,该定时任务会停止。为了避免任务被异常中断,可以使用try/catch来捕获异常并进行处理。在给定的示例代码中,使用了try/catch来捕获异常,并在捕获到异常时输出错误信息。 该方法的用法通常是将一个Runnable对象传递给scheduleAtFixedRate()方法,该Runnable对象中定义了要执行的任务逻辑。在给定的示例代码中,使用了一个匿名内部类实现了Runnable接口,并在run()方法中调用了service.backupData(1)方法。 总结起来,ScheduledExecutorService.scheduleAtFixedRate方法可用于定时执行任务,并且可以通过try/catch来处理异常并保证任务的持续执行。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ScheduledExecutorServicescheduleAtFixedRate分析](https://blog.csdn.net/u010523770/article/details/118368503)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【向】一次使用ScheduledExecutorService.scheduleAtFixedRate()的](https://blog.csdn.net/aqin1012/article/details/123575274)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [serial-executor-service:ScheduledExecutorService 的测试实用程序实现](https://download.csdn.net/download/weixin_42156940/19488313)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AQin1012

求小鱼干呢~~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值