如何在Java中实现异步任务组合

CompletableFuture 提供异步任务组合能力,支持串行(thenApply、thenCompose)、并行(thenCombine、allOf)及异常处理(exceptionally、handle),需注意 allOf 返回 Void 且应避免阻塞调用。

在Java中实现异步任务组合,主要依赖于 CompletableFuture 类。它提供了丰富的API来组合多个异步操作,支持链式调用、并行执行、结果合并等场景,是现代Java并发编程的核心工具之一。

使用 CompletableFuture 实现基本异步任务

CompletableFuture 默认使用 ForkJoinPool 的公共线程池执行任务,也可以指定自定义线程池。

// 基本异步任务

CompletableFuture task1 = CompletableFuture.supplyAsync(() -> {

  try { Thread.sleep(1000); } catch (InterruptedException e) {}

  return "Result from task1";

});

串行组合:thenApply、thenCompose

当一个任务依赖前一个任务的结果时,可以使用串行组合方法。

  • thenApply:对上一步结果进行转换,返回值作为新结果
  • thenCompose:用于链式调用另一个 CompletableFuture,适合扁平化嵌套异步调用

CompletableFuture chained = task1.thenApply(result -> result + " processed");

CompletableFuture composed = task1.thenCompose(result ->

  CompletableFuture.supplyAsync(() -> result + " composed"));

并行组合:thenCombine、allOf

多个独立异步任务可并行执行,并在全部完成后合并结果。

  • thenCombine:组合两个异步任务的结果
  • CompletableFuture.allOf:等待所有任务完成(注意:返回 CompletableFuture

CompletableFuture task2 = CompletableFuture.supplyAsync(() -> "Task2 result");

CompletableFuture combined = task1.thenCombine(task2, (r1, r2) -> r1 + " | " + r2);

// 等待多个任务完成

CompletableFuture allDone = CompletableFuture.allOf(task1, task2, task3);

allDone.thenRun(() -> System.out.println("All tasks finished")); // 注意触发动作需显式调用

异常处理与默认值

异步任务可能失败,需通过异常处理机制保证流程健壮性。

  • exceptionally:捕获异常并提供默认值
  • handle:无论成功或失败都会执行,可用于统一处理

CompletableFuture withFallback = task1.exceptionally(ex -> "Fallback value");

CompletableFuture handled = task1.handle((result, ex) -> {

  if (ex != null) return "Error occurred";

  return result + " handled";

});

基本上就这些。CompletableFuture 提供了灵活的异步组合能力,合理使用 thenApply、thenCombine、allOf 和异常处理方法,能有效管理复杂异步流程。关键是理解每个组合方法的行为差异,避免阻塞调用如 get(),保持非阻塞风格。不复杂但容易忽略的是 allOf 返回的 Void,需要手动获取原始任务结果。