Kotlin-Demo/bunny/async/CompletableFutureTest2.java

19 lines
661 B
Java
Raw Normal View History

2024-08-20 13:52:40 +08:00
package bunny.async;
import java.util.concurrent.CompletableFuture;
public class CompletableFutureTest2 {
public static void main(String[] args) throws Exception {
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
// 传入所有的任务返回(CompletableFuture)
// 等待所有任务都完成
CompletableFuture<Void> allFutures = CompletableFuture.allOf(future1, future2);
// 阻塞线程
allFutures.join();
System.out.println("所有任务都完成了。。。");
}
}