面试题答案
一键面试- 思路:
- 利用
CompletableFuture
的异常处理方法,如exceptionally
来捕获单个任务的异常。 - 在捕获异常后,可以继续执行后续任务,通过将异常信息作为特殊的“结果”传递给后续任务。
- 最后,通过
join
或get
方法获取所有任务的最终结果或异常,并进行汇总。
- 利用
- 示例代码:
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
public class CompletableFutureExceptionHandling {
public static void main(String[] args) {
List<CompletableFuture<String>> futures = new ArrayList<>();
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
System.out.println("Task 1 is running");
return "Result of Task 1";
});
CompletableFuture<String> future2 = future1.thenApplyAsync(result -> {
System.out.println("Task 2 is running with input: " + result);
if ("Result of Task 1".equals(result)) {
throw new RuntimeException("Task 2 has an error");
}
return "Result of Task 2";
}).exceptionally(ex -> {
System.out.println("Task 2 caught an exception: " + ex.getMessage());
return "Exception in Task 2: " + ex.getMessage();
});
CompletableFuture<String> future3 = future2.thenApplyAsync(result -> {
System.out.println("Task 3 is running with input: " + result);
return "Result of Task 3";
});
futures.add(future1);
futures.add(future2);
futures.add(future3);
List<String> results = new ArrayList<>();
for (CompletableFuture<String> future : futures) {
try {
results.add(future.join());
} catch (Exception e) {
results.add("Exception in overall handling: " + e.getMessage());
}
}
System.out.println("Final results: " + results);
}
}
在上述代码中:
future1
是第一个任务,正常返回结果。future2
依赖future1
的结果,这里故意抛出异常,通过exceptionally
方法捕获异常并返回包含异常信息的字符串。future3
依赖future2
的结果,无论future2
是正常返回还是抛出异常,都会继续执行。- 最后通过遍历
futures
列表,使用join
方法获取每个任务的结果(或异常信息)并汇总到results
列表中。