异步编程-CompletableFuture 2024-11-07 程序之旅,记录 暂无评论 114 次阅读 `CompletableFuture` 是 Java 8 引入的一个类,用于处理异步编程。它实现了 `Future` 接口,并且提供了更强大的功能,比如组合多个异步操作、处理异常、以及在异步操作完成后执行回调等。 ### 基本用法 #### 1. 创建 `CompletableFuture` ```java CompletableFuture future = new CompletableFuture<>(); ``` #### 2. 完成 `CompletableFuture` ```java future.complete("Hello, World!"); ``` #### 3. 获取结果 ```java String result = future.get(); // 阻塞等待结果 ``` ### 异步执行任务 #### 1. 使用 `runAsync` 执行无返回值的任务 ```java CompletableFuture future = CompletableFuture.runAsync(() -> { System.out.println("Running in a separate thread"); }); ``` #### 2. 使用 `supplyAsync` 执行有返回值的任务 ```java CompletableFuture future = CompletableFuture.supplyAsync(() -> { return "Hello, World!"; }); ``` ### 处理结果 #### 1. `thenApply`:处理结果并返回新的结果 ```java CompletableFuture future = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture resultFuture = future.thenApply(s -> s + " World"); ``` #### 2. `thenAccept`:处理结果但不返回新的结果 ```java CompletableFuture future = CompletableFuture.supplyAsync(() -> "Hello"); future.thenAccept(s -> System.out.println(s + " World")); ``` #### 3. `thenRun`:在任务完成后执行某个操作 ```java CompletableFuture future = CompletableFuture.runAsync(() -> { System.out.println("Running task"); }); future.thenRun(() -> System.out.println("Task completed")); ``` ### 异常处理 #### 1. `exceptionally`:处理异常并返回默认值 ```java CompletableFuture future = CompletableFuture.supplyAsync(() -> { if (true) throw new RuntimeException("Oops"); return "Hello"; }); CompletableFuture resultFuture = future.exceptionally(ex -> "Default Value"); ``` #### 2. `handle`:无论是否发生异常,都会执行 ```java CompletableFuture future = CompletableFuture.supplyAsync(() -> { if (true) throw new RuntimeException("Oops"); return "Hello"; }); CompletableFuture resultFuture = future.handle((result, ex) -> { if (ex != null) { return "Error: " + ex.getMessage(); } return result; }); ``` ### 组合多个 `CompletableFuture` #### 1. `thenCompose`:将一个 `CompletableFuture` 的结果作为另一个 `CompletableFuture` 的输入 ```java CompletableFuture future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture future2 = future1.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World")); ``` #### 2. `thenCombine`:将两个 `CompletableFuture` 的结果合并 ```java CompletableFuture future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture future2 = CompletableFuture.supplyAsync(() -> " World"); CompletableFuture combinedFuture = future1.thenCombine(future2, (s1, s2) -> s1 + s2); ``` ### 等待多个 `CompletableFuture` 完成 #### 1. `allOf`:等待所有 `CompletableFuture` 完成 ```java CompletableFuture future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture future2 = CompletableFuture.supplyAsync(() -> "World"); CompletableFuture allFutures = CompletableFuture.allOf(future1, future2); allFutures.thenRun(() -> { System.out.println("All tasks completed"); }); ``` #### 2. `anyOf`:等待任意一个 `CompletableFuture` 完成 ```java CompletableFuture future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture future2 = CompletableFuture.supplyAsync(() -> "World"); CompletableFuture anyFuture = CompletableFuture.anyOf(future1, future2); anyFuture.thenAccept(result -> { System.out.println("First completed: " + result); }); ``` ### 总结 `CompletableFuture` 提供了丰富的 API 来处理异步编程中的各种场景,包括任务的执行、结果的处理、异常的处理、以及多个任务的组合和等待。通过合理使用这些 API,可以大大简化异步编程的复杂性。 打赏: 微信, 支付宝 标签: java 本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。