18 lines
570 B
Java
18 lines
570 B
Java
package bunny.async;
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
public class CompletableFutureTest4 {
|
|
public static void main(String[] args) {
|
|
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> 42);
|
|
|
|
// 链式调用和JavaScript中promise有点像
|
|
CompletableFuture<String> transformedFuture = future
|
|
.thenApply(result -> "Result1: " + result)
|
|
.thenApply(result -> "Result2: " + result);
|
|
|
|
String result = transformedFuture.join();
|
|
System.out.println(result);
|
|
}
|
|
}
|