You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java中ScheduledExecutorService为何执行一次后停止?

问题根源

ScheduledExecutorService的调度规则是:如果定时任务抛出未捕获的RuntimeException或Error,调度器会自动取消该任务的后续执行,不会再继续调度。这就是你的任务只执行一次就停止的核心原因——不管用scheduleAtFixedRate还是scheduleWithFixedDelay,这个规则都适用。

解决方案

解决方法很直接:在任务内部添加全局异常捕获,确保所有可能抛出的异常都被处理,不让异常扩散到调度器层面。

修改后的代码示例:

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class SchedulerTest {
    public static void main(String[] args) {
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        
        scheduler.scheduleAtFixedRate(() -> {
            try {
                System.out.println("Running task at: " + System.currentTimeMillis());
                if (true) { 
                    throw new RuntimeException("Exception during task execution");
                }
            } catch (Exception e) {
                // 可根据需求添加日志记录、告警等异常处理逻辑
                System.err.println("Task execution failed: " + e.getMessage());
            }
        }, 0, 5, TimeUnit.SECONDS);
    }
}
补充说明
  • 移除异常后任务恢复正常,是因为没有未捕获异常抛出,调度器会按照设定的间隔持续执行任务。
  • scheduleAtFixedRate和scheduleWithFixedDelay的区别仅在于间隔计算方式(前者以上次任务开始时间算间隔,后者以上次任务结束时间算),但两者在处理未捕获异常的逻辑上完全一致,所以替换方法无法解决问题。

内容的提问来源于stack exchange,提问作者Hanan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 06:30:04