【进阶】- CompletableFuture
2021-02-05 本文已影响0人
lconcise
简单使用
使用Future获得异步执行结果时,要么调用阻塞方法get(),要么轮询isDone()是否为true,这两种方法都不是很好,因为主线程也会被迫等待。
从Java8开始引入了CompletableFuture,它针对Future做了改进,可以传入回调对象,当异步任务完成或者发生异常时,自动调用回调对象的回调方法。
public class Demo01 {
public static void main(String[] args) throws Exception {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> study());
// 如果执行成功
future.thenAccept(str -> System.out.println(str));
// 如果抛出异常
future.exceptionally(e -> {
e.printStackTrace();
return null;
});
System.out.println("==");
// 主线程不要立刻结束,否则CompletableFuture默认使用的线程池会立刻关闭:
TimeUnit.SECONDS.sleep(2);
}
public static String study() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "但行其事,不问前程;莫忘初心,方得始终。";
}
}
执行结果:
==
但行其事,不问前程;莫忘初心,方得始终。
CompletableFuture

CompletableFuture 不经实现了Future,还实现了CompletionStage。
CompletableStage
A stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes.
CompletableFuture 实现串行操作
public class Demo02 {
public static void main(String[] args) throws Exception {
// 串行任务
// 任务一:初中学习
CompletableFuture<Void> future01 = CompletableFuture.runAsync(() -> studyMiddleSchool());
// 任务二:高中学习
CompletableFuture<Void> future02 = future01.thenRunAsync(() -> studyHighSchool());
// 任务三:大学学习
CompletableFuture<String> future03 = future02.thenApplyAsync(str -> studyColleage());
System.out.println("==");
future03.thenAcceptAsync(str -> System.out.println(str));
System.out.println("==");
TimeUnit.SECONDS.sleep(4);
}
public static void studyMiddleSchool() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("初中学习");
}
public static void studyHighSchool() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("高中学习");
}
public static String studyColleage() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("大学学习");
return "步入社会";
}
}
执行结果:
==
==
初中学习
高中学习
大学学习
步入社会
Completable OR汇聚操作
public class Demo03 {
public static void main(String[] args) throws Exception {
CompletableFuture<String> future01 = CompletableFuture.supplyAsync(() -> buyFood("面条"));
CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> buyFood("米饭"));
// OR 汇聚关系,米饭/面条,米饭先来,就吃米饭;面条先来,就先吃面条;
CompletableFuture<Object> future03 = CompletableFuture.anyOf(future01, future02);
future03.thenAcceptAsync(food -> eat((String) food));
TimeUnit.SECONDS.sleep(5);
}
public static String buyFood(String food) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("购买: " + food);
return food;
}
public static void eat(String food) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("吃饭:" + food);
}
}
执行结果:随机结果:吃米饭、吃面条。
购买: 面条
购买: 米饭
吃饭:面条
参考博文:https://www.liaoxuefeng.com/wiki/1252599548343744/1306581182447650