簡(jiǎn)單使用
使用Future獲得異步執(zhí)行結(jié)果時(shí),要么調(diào)用阻塞方法get()晰绎,要么輪詢isDone()是否為true寓落,這兩種方法都不是很好,因?yàn)橹骶€程也會(huì)被迫等待寒匙。
從Java8開(kāi)始引入了CompletableFuture零如,它針對(duì)Future做了改進(jìn)躏将,可以傳入回調(diào)對(duì)象,當(dāng)異步任務(wù)完成或者發(fā)生異常時(shí)考蕾,自動(dòng)調(diào)用回調(diào)對(duì)象的回調(diào)方法祸憋。
public class Demo01 {
public static void main(String[] args) throws Exception {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> study());
// 如果執(zhí)行成功
future.thenAccept(str -> System.out.println(str));
// 如果拋出異常
future.exceptionally(e -> {
e.printStackTrace();
return null;
});
System.out.println("==");
// 主線程不要立刻結(jié)束,否則CompletableFuture默認(rèn)使用的線程池會(huì)立刻關(guān)閉:
TimeUnit.SECONDS.sleep(2);
}
public static String study() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "但行其事肖卧,不問(wèn)前程蚯窥;莫忘初心,方得始終塞帐。";
}
}
執(zhí)行結(jié)果:
==
但行其事拦赠,不問(wèn)前程;莫忘初心葵姥,方得始終荷鼠。
CompletableFuture
image.png
CompletableFuture 不經(jīng)實(shí)現(xiàn)了Future,還實(shí)現(xiàn)了CompletionStage榔幸。
CompletableStage
A stage of a possibly asynchronous computation, that performs an action or computes a value when another CompletionStage completes.
CompletableFuture 實(shí)現(xiàn)串行操作
public class Demo02 {
public static void main(String[] args) throws Exception {
// 串行任務(wù)
// 任務(wù)一:初中學(xué)習(xí)
CompletableFuture<Void> future01 = CompletableFuture.runAsync(() -> studyMiddleSchool());
// 任務(wù)二:高中學(xué)習(xí)
CompletableFuture<Void> future02 = future01.thenRunAsync(() -> studyHighSchool());
// 任務(wù)三:大學(xué)學(xué)習(xí)
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("初中學(xué)習(xí)");
}
public static void studyHighSchool() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("高中學(xué)習(xí)");
}
public static String studyColleage() {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("大學(xué)學(xué)習(xí)");
return "步入社會(huì)";
}
}
執(zhí)行結(jié)果:
==
==
初中學(xué)習(xí)
高中學(xué)習(xí)
大學(xué)學(xué)習(xí)
步入社會(huì)
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 匯聚關(guān)系允乐,米飯/面條,米飯先來(lái)削咆,就吃米飯牍疏;面條先來(lái),就先吃面條拨齐;
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("購(gòu)買: " + food);
return food;
}
public static void eat(String food) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("吃飯:" + food);
}
}
執(zhí)行結(jié)果:隨機(jī)結(jié)果:吃米飯鳞陨、吃面條。
購(gòu)買: 面條
購(gòu)買: 米飯
吃飯:面條
參考博文:https://www.liaoxuefeng.com/wiki/1252599548343744/1306581182447650