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

如何在至少一个Future执行失败时关闭ExecutorService?

实现“任意任务失败则关闭ExecutorService”的方案

JDK原生的ExecutorService没有内置“任务失败即关闭”的特性,需要自行实现监听逻辑。核心思路是为每个提交的任务添加结果监听,一旦捕获到执行异常,立即触发线程池关闭。

方案一:基于原生Future监听

通过在提交任务后启动监听线程,检查Future的执行结果,捕获异常时关闭线程池:

ExecutorService executorService = Executors.newSingleThreadExecutor();

public void runCommand(String command) {
    Runnable runnable = () -> {
        logger.info("执行任务: {}", command);
        // 模拟任务失败场景
        if ("fail".equals(command)) {
            throw new RuntimeException("任务执行失败");
        }
    };

    Future<?> future = executorService.submit(runnable);

    // 启动监听线程处理任务结果
    new Thread(() -> {
        try {
            future.get();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (ExecutionException e) {
            logger.error("任务执行失败,关闭ExecutorService", e.getCause());
            // 立即关闭线程池,中断正在执行的任务并拒绝新任务
            executorService.shutdownNow();
        }
    }).start();
}

方案二:使用CompletableFuture简化监听(Java 8+)

利用CompletableFuture的回调机制,无需额外启动线程即可监听任务状态:

ExecutorService executorService = Executors.newSingleThreadExecutor();

public void runCommand(String command) {
    Runnable runnable = () -> {
        logger.info("执行任务: {}", command);
        if ("fail".equals(command)) {
            throw new RuntimeException("任务执行失败");
        }
    };

    CompletableFuture.runAsync(runnable, executorService)
            .whenComplete((unused, throwable) -> {
                if (throwable != null) {
                    logger.error("任务执行失败,关闭ExecutorService", throwable);
                    executorService.shutdownNow();
                }
            });
}

关键说明

  • shutdownNow()会尝试中断当前执行的任务,并清空等待队列,适合需要立即停止所有任务的场景;若希望等待当前任务完成后再关闭,可替换为shutdown()(但shutdown()不会阻止队列中待执行的任务)。
  • 线程池的关闭方法(shutdown()/shutdownNow())本身是线程安全的,无需担心多任务同时触发关闭的冲突。
  • JDK标准库中没有提供具备该特性的ExecutorService子类,必须通过上述自定义监听逻辑实现需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 03:46:08