public class CompletableFuture2 {
public static void main(String[] args) throws InterruptedException {
// thenAcceptBoth();
// acceptEither();
// runAfterBoth();
// runAfterEither();
// combine();
compose();
Thread.currentThread().join();
}
/**
* 前一个CompletableFuture执行完后的结果,作为参数传递给后面的Function
*/
private static void compose(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the compose1");
sleep(5);
System.out.println("end the compose1");
return "hello1";
}).thenCompose((s) -> CompletableFuture.supplyAsync(() -> {
System.out.println("start the compose2");
sleep(3);
System.out.println("end the compose2");
return s.length();
})).thenAccept(System.out::println);
}
/**
* 两个都执行完后,再执行后面的BiFunction,BiFunction是有两个参数一个返回值的
*/
private static void combine(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the combine1");
sleep(5);
System.out.println("end the combine1");
return "hello1";
}).thenCombine(CompletableFuture.supplyAsync(() -> {
System.out.println("start the combine2");
sleep(3);
System.out.println("end the combine2");
return 100;
}), (s, i) -> s.length() > i
).whenComplete((v, t) -> {
System.out.println(v);
});
}
/**
* 其中有任何一个执行完后,执行后面的runnable
*/
private static void runAfterEither(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the runAfterEither1");
sleep(5);
System.out.println("end the runAfterEither1");
return "hello1";
}).runAfterEither(CompletableFuture.supplyAsync(() -> {
System.out.println("start the runAfterEither2");
sleep(3);
System.out.println("end the runAfterEither2");
return "hello1";
}),()-> System.out.println("======"));
}
/**
* 两个都执行完后,执行后面的runnable
*/
private static void runAfterBoth(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the runAfterBoth1");
sleep(5);
System.out.println("end the runAfterBoth1");
return "hello1";
}).runAfterBoth(CompletableFuture.supplyAsync(() -> {
System.out.println("start the runAfterBoth2");
sleep(3);
System.out.println("end the runAfterBoth2");
return "hello1";
}), () -> System.out.println("=====")
);
}
/**
* 其中有任何一个执行完后,执行后面的consumer
*/
private static void acceptEither(){
CompletableFuture.supplyAsync(() -> {
System.out.println("start the acceptEither1");
sleep(5);
System.out.println("end the acceptEither1");
return "hello1";
}).acceptEither(CompletableFuture.supplyAsync(() -> {
System.out.println("start the acceptEither2");
sleep(3);
System.out.println("end the acceptEither2");
return "hello2";
}), (s) -> {
System.out.println(s);
});
}
/**
* thenAcceptBoth 两个都执行完后,执行后面的BiConsumer,BiConsumer两个参数,无返回值
*/
private static void thenAcceptBoth(){
CompletableFuture.supplyAsync(()->{
System.out.println("start the supplyAsync");
sleep(5);
System.out.println("end the supplyAsync");
return "hello";
}).thenAcceptBoth(CompletableFuture.supplyAsync(()->{
System.out.println("start the thenAcceptBoth");
sleep(3);
System.out.println("end the thenAcceptBoth");
return 100;
}),(s,i)->{
System.out.println(s+" ===" + i);
});
}
private static void sleep(int sec){
try {
TimeUnit.SECONDS.sleep(sec);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}