thenCombine() // 两个任务一起执行完成后结果汇总
public static void thenCombine() {
CompletableFuture<String> fi = CompletableFuture.supplyAsync(() -> {
ThreadUtil.printThreadInfo("厨师炒菜");
ThreadUtil.sleep(200);
return "大头菜";
}).thenCombine(CompletableFuture.supplyAsync(() -> {
ThreadUtil.printThreadInfo("服务员煮饭");
ThreadUtil.sleep(100);
return " 米饭";
}), (a, b) -> {
ThreadUtil.printThreadInfo("服务员拿菜和饭");
ThreadUtil.sleep(50);
return a + b;
});
ThreadUtil.printThreadInfo("吃饭 " + fi.join());
}
allOf() 多个任务执行结果合并
public static void allOf() {
List<String> stringList = Lists.newArrayList();
CompletableFuture<String>[] list = new CompletableFuture[3];
CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(300);
ThreadUtil.printThreadInfo("厨师炒菜");
return "大头菜";
}).whenComplete((v, e) -> {
stringList.add(v);
});
list[0] = f1;
CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(200);
ThreadUtil.printThreadInfo("服务员煮饭");
return " 煮饭";
}).whenComplete((v, e) -> {
stringList.add(v);
});
list[1] = f2;
CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(100);
ThreadUtil.printThreadInfo("服务员打饭");
return " 打饭";
}).whenComplete((v, e) -> {
stringList.add(v);
});
list[2] = f3;
CompletableFuture.allOf(list).join();
stringList.forEach(str->{
ThreadUtil.printThreadInfo(str);
});
}
anyOf() 多个任务中一个执行完就返回
public static void anyOf() {
List<String> stringList = Lists.newArrayList();
CompletableFuture<String>[] list = new CompletableFuture[3];
CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(300);
ThreadUtil.printThreadInfo("厨师炒菜");
return "大头菜";
}).whenComplete((v, e) -> {
stringList.add(v);
});
list[0] = f1;
CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(200);
ThreadUtil.printThreadInfo("服务员煮饭");
return " 煮饭";
}).whenComplete((v, e) -> {
stringList.add(v);
});
list[1] = f2;
CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> {
ThreadUtil.sleep(100);
ThreadUtil.printThreadInfo("服务员打饭");
return " 打饭";
}).whenComplete((v, e) -> {
stringList.add(v);
});
list[2] = f3;
CompletableFuture.anyOf(list).join();
stringList.forEach(str->{
ThreadUtil.printThreadInfo(str);
});
}
applyToEither() 两个任务谁先执行完谁就返回
public static void applyToEither() {
CompletableFuture<String> fi = CompletableFuture.supplyAsync(() -> {
ThreadUtil.printThreadInfo("厨师炒菜开始");
ThreadUtil.sleep(200);
ThreadUtil.printThreadInfo("厨师炒菜完成");
return "大头菜";
}).applyToEither(CompletableFuture.supplyAsync(() -> {
ThreadUtil.printThreadInfo("服务员打饭开始");
ThreadUtil.sleep(100);
ThreadUtil.printThreadInfo("服务员打饭完成");
return " 米饭";
}),a->a);
ThreadUtil.printThreadInfo("吃饭 " + fi.join());
}
exceptionally() 异常处理
public static void exceptionally() {
CompletableFuture<String> fi = CompletableFuture.supplyAsync(() -> {
ThreadUtil.printThreadInfo("厨师炒菜开始");
ThreadUtil.sleep(200);
ThreadUtil.printThreadInfo("厨师炒菜完成");
return "大头菜";
}).applyToEither(CompletableFuture.supplyAsync(() -> {
ThreadUtil.printThreadInfo("服务员打饭开始");
ThreadUtil.sleep(100);
ThreadUtil.printThreadInfo("服务员打饭完成");
return "米饭";
}),a->{
ThreadUtil.printThreadInfo("异常");
if(a.equals("米饭")){
throw new RuntimeException("程序异常");
}
return "正常";
}).exceptionally(e->{
ThreadUtil.printThreadInfo("异常程序终止");
return "程序终止";
});
ThreadUtil.printThreadInfo("吃饭 " + fi.join());
}