19 lines
662 B
Java
19 lines
662 B
Java
package bunny.async;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
public class CompletableFutureTest3 {
|
|
public static void main(String[] args) {
|
|
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
|
|
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
|
|
|
|
// 可以传入多个任务(CompletableFuture)
|
|
// 任意一个任务完成
|
|
CompletableFuture<Object> anyFuture = CompletableFuture.anyOf(future1, future2);
|
|
|
|
// 阻塞获取结果
|
|
Object result = anyFuture.join();
|
|
System.out.println("任意一个任务完成: " + result);
|
|
}
|
|
}
|