原创转载请注明出处:http://agilestyle.iteye.com/blog/2427515
runAfterBoth
private static void testRunAfterBoth() {
CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
return 1;
}).runAfterBoth(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
return 2;
}), () -> System.out.println("action ended..."));
}
Console Output
applyToEither
private static void testApplyToEither() {
CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
return CompletableFutureTest6.get();
}).applyToEither(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
return CompletableFutureTest6.get();
}), v -> v * 10)
.thenAccept(System.out::println);
}
Console Output
acceptEither
private static void testAcceptEither() {
CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
return CompletableFutureTest6.get();
}).acceptEither(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
return CompletableFutureTest6.get();
}), System.out::println);
}
Console Output
runAfterEither
private static void testRunAfterEither() {
CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
return CompletableFutureTest6.get();
}).runAfterEither(CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread().getName() + " is running");
return CompletableFutureTest6.get();
}), () -> System.out.println("action ended..."));
}
Console Output
anyOf
private static void testAnyOf() {
List<CompletableFuture<Double>> result = Stream.of(1, 2, 3, 4, 5)
.map(i -> CompletableFuture.supplyAsync(CompletableFutureTest6::get))
.collect(toList());
CompletableFuture.anyOf(result.toArray(new CompletableFuture[result.size()]))
.thenRun(() -> System.out.println("action ended..."));
}
Console Output
allOf
private static void testAllOf() {
List<CompletableFuture<Double>> result = Stream.of(1, 2, 3, 4, 5)
.map(i -> CompletableFuture.supplyAsync(CompletableFutureTest6::get))
.collect(toList());
CompletableFuture.allOf(result.toArray(new CompletableFuture[result.size()]))
.thenRun(() -> System.out.println("action ended..."));
}
Console Output