1
0

更新 Java_3-5.md

This commit is contained in:
8ga 2025-09-11 09:38:21 +08:00
parent 2540361bb8
commit dc072b8b8a

View File

@ -16,7 +16,9 @@
**继续追问**
<span style="color: red">假定有3个Runnable对象分别是task1、task2、task3作为查询函数对象如何调度或者编排这3个task</span>
<span style="color: red">
假定有3个Runnable对象分别是task1、task2、task3作为查询函数对象如何调度或者编排这3个task
</span>
```java
Runnable task1 = () -> System.out.println("Task 1");
@ -26,12 +28,14 @@ Runnable task3 = () -> System.out.println("Task 3");
**方案一、CompletableFuture**
<span style="color: green">
使用 CompletableFuture 的 runAsync 函数声明3个异步任务。通过 CompletableFuture 的 allOf 函数添加3个异步任务调用 get 函数阻塞。
</span>
```java
// 使用 CompletableFuture 的 runAsync 函数声明3个异步任务
CompletableFuture<Void> cf1 = CompletableFuture.runAsync(task1);
CompletableFuture<Void> cf2 = CompletableFuture.runAsync(task2);
CompletableFuture<Void> cf3 = CompletableFuture.runAsync(task3);
// 通过 CompletableFuture 的 allOf 函数添加3个异步任务调用 get 函数阻塞
CompletableFuture.allOf(cf1, cf2, cf3).get();
```