更新 Java_3-5.md
This commit is contained in:
parent
2540361bb8
commit
dc072b8b8a
10
Java_3-5.md
10
Java_3-5.md
@ -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
|
```java
|
||||||
Runnable task1 = () -> System.out.println("Task 1");
|
Runnable task1 = () -> System.out.println("Task 1");
|
||||||
@ -26,12 +28,14 @@ Runnable task3 = () -> System.out.println("Task 3");
|
|||||||
|
|
||||||
**方案一、CompletableFuture**
|
**方案一、CompletableFuture**
|
||||||
|
|
||||||
|
<span style="color: green">
|
||||||
|
使用 CompletableFuture 的 runAsync 函数,声明3个异步任务。通过 CompletableFuture 的 allOf 函数,添加3个异步任务,调用 get 函数阻塞。
|
||||||
|
</span>
|
||||||
|
|
||||||
```java
|
```java
|
||||||
// 使用 CompletableFuture 的 runAsync 函数,声明3个异步任务
|
|
||||||
CompletableFuture<Void> cf1 = CompletableFuture.runAsync(task1);
|
CompletableFuture<Void> cf1 = CompletableFuture.runAsync(task1);
|
||||||
CompletableFuture<Void> cf2 = CompletableFuture.runAsync(task2);
|
CompletableFuture<Void> cf2 = CompletableFuture.runAsync(task2);
|
||||||
CompletableFuture<Void> cf3 = CompletableFuture.runAsync(task3);
|
CompletableFuture<Void> cf3 = CompletableFuture.runAsync(task3);
|
||||||
// 通过 CompletableFuture 的 allOf 函数,添加3个异步任务,调用 get 函数阻塞
|
|
||||||
CompletableFuture.allOf(cf1, cf2, cf3).get();
|
CompletableFuture.allOf(cf1, cf2, cf3).get();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user