詳解Java8中CompletableFuture類(lèi)的使用
Java 8中引入了CompletableFuture類(lèi),它是一種方便的異步編程工具,可以處理各種異步操作,如網(wǎng)絡(luò)請(qǐng)求、文件IO和數(shù)據(jù)庫(kù)操作等。它是Java的Future接口的擴(kuò)展,提供了一些有用的方法來(lái)創(chuàng)建、操作和組合異步操作。本文將詳細(xì)介紹CompletableFuture的使用方式。
創(chuàng)建CompletableFuture
CompletableFuture提供了多種方法來(lái)創(chuàng)建CompletableFuture對(duì)象,如:
1.使用CompletableFuture.supplyAsync()方法創(chuàng)建異步執(zhí)行的Supplier,Supplier中的代碼會(huì)在異步線程中執(zhí)行,代碼執(zhí)行完畢后,CompletableFuture將會(huì)得到執(zhí)行結(jié)果。
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
2.使用CompletableFuture.runAsync()方法創(chuàng)建異步執(zhí)行的Runnable,Runnable中的代碼會(huì)在異步線程中執(zhí)行,代碼執(zhí)行完畢后,CompletableFuture將會(huì)得到null作為執(zhí)行結(jié)果。
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
//異步執(zhí)行的代碼
});3.使用CompletableFuture.completedFuture()方法創(chuàng)建一個(gè)已經(jīng)完成的CompletableFuture對(duì)象。
CompletableFuture<String> future = CompletableFuture.completedFuture("Hello");
4.使用CompletableFuture的構(gòu)造方法創(chuàng)建CompletableFuture對(duì)象。
CompletableFuture<String> future = new CompletableFuture<>();
這種方式通常用于在執(zhí)行某個(gè)操作之前創(chuàng)建一個(gè)CompletableFuture對(duì)象,并將其傳遞給其他方法,以便在異步操作完成后將結(jié)果傳遞回來(lái)。
處理CompletableFuture的結(jié)果
當(dāng)異步操作完成時(shí),可以通過(guò)CompletableFuture的get()方法獲取執(zhí)行結(jié)果。
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); String result = future.get(); System.out.println(result); //輸出"Hello"
但是,get()方法是一個(gè)阻塞的方法,它會(huì)一直等待異步操作完成,并返回結(jié)果或者拋出異常。如果你不想阻塞當(dāng)前線程,你可以使用回調(diào)函數(shù)的方式來(lái)處理CompletableFuture的結(jié)果。
1.使用thenApply()方法處理CompletableFuture的結(jié)果。
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> future2 = future.thenApply(result -> result + " World"); System.out.println(future2.get()); //輸出"Hello World"
在這個(gè)例子中,我們使用thenApply()方法來(lái)處理CompletableFuture的結(jié)果。它接受一個(gè)Function函數(shù),用于將CompletableFuture的結(jié)果轉(zhuǎn)換為另一個(gè)值。
2.使用thenAccept()方法處理CompletableFuture的結(jié)果。
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello"); future.thenAccept(result -> System.out.println(result + " World"));
在這個(gè)例子中,我們使用thenAccept()方法來(lái)處理CompletableFuture的結(jié)果。它接受一個(gè)Consumer函數(shù),用于處理CompletableFuture的結(jié)果,但是不返回任何結(jié)果。
3.使用thenCompose()方法組合多個(gè)CompletableFuture。
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
CompletableFuture<String> future3 = future1.thenCompose(result1 -> future2.thenApply(result2 -> result1 + " " + result2));
try {
System.out.println(future3.get());
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
在這個(gè)例子中,我們使用thenCompose()方法來(lái)組合多個(gè)CompletableFuture對(duì)象。它接受一個(gè)Function函數(shù),該函數(shù)將CompletableFuture的結(jié)果轉(zhuǎn)換為另一個(gè)CompletableFuture對(duì)象。在這個(gè)例子中,我們先使用future1來(lái)創(chuàng)建一個(gè)新的CompletableFuture對(duì)象,然后將future2的結(jié)果作為參數(shù)傳遞給該對(duì)象的處理函數(shù)。
4.使用thenCombine()方法組合多個(gè)CompletableFuture。
CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 10); CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> 20); CompletableFuture<Integer> future3 = future1.thenCombine(future2, (result1, result2) -> result1 + result2); System.out.println(future3.get()); //輸出30
在這個(gè)例子中,我們使用thenCombine()方法來(lái)組合多個(gè)CompletableFuture對(duì)象。它接受另一個(gè)CompletableFuture對(duì)象和一個(gè)BiFunction函數(shù),該函數(shù)用于將兩個(gè)CompletableFuture的結(jié)果合并為一個(gè)新的結(jié)果。
處理CompletableFuture的異常
當(dāng)CompletableFuture執(zhí)行過(guò)程中出現(xiàn)異常時(shí),我們需要使用exceptionally()方法來(lái)處理異常。
CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
throw new RuntimeException("異常信息");
});
future.exceptionally(ex -> {
System.out.println(ex.getMessage()); //輸出"異常信息"
return 0;
});在這個(gè)例子中,我們使用exceptionally()方法來(lái)處理CompletableFuture的異常。它接受一個(gè)Function函數(shù),用于處理異常并返回一個(gè)默認(rèn)值。
等待多個(gè)CompletableFuture執(zhí)行完畢
有時(shí)我們需要等待多個(gè)CompletableFuture對(duì)象執(zhí)行完畢后再繼續(xù)執(zhí)行下一步操作。我們可以使用CompletableFuture的allOf()方法或anyOf()方法來(lái)等待多個(gè)CompletableFuture對(duì)象執(zhí)行完畢。
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello"); CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World"); CompletableFuture<Void> allFuture = CompletableFuture.allOf(future1, future2); allFuture.get(); CompletableFuture<Object> anyFuture = CompletableFuture.anyOf(future1, future2); System.out.println(anyFuture.get()); //輸出"Hello"或"World"
在這個(gè)例子中,我們使用allOf()方法來(lái)等待所有的CompletableFuture對(duì)象執(zhí)行完畢,并使用anyOf()方法來(lái)等待任何一個(gè)CompletableFuture對(duì)象執(zhí)行完畢。
總結(jié)
CompletableFuture是Java 8中提供的一種非常方便的異步編程工具,它可以處理各種異步操作,并提供了豐富的方法來(lái)創(chuàng)建、操作和組合CompletableFuture對(duì)象。在實(shí)際應(yīng)用中,我們可以根據(jù)實(shí)際需求選擇合適的方法來(lái)使用CompletableFuture,提高代碼的性能和可讀性。
到此這篇關(guān)于詳解Java8中CompletableFuture類(lèi)的使用的文章就介紹到這了,更多相關(guān)Java8 CompletableFuture類(lèi)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis使用RedisTemplate模板類(lèi)的常用操作方式
這篇文章主要介紹了Redis使用RedisTemplate模板類(lèi)的常用操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
解析Oracle數(shù)據(jù)庫(kù)中的對(duì)象集合schema
這篇文章主要介紹了Oracle數(shù)據(jù)庫(kù)中的對(duì)象集合schema,是Oracle數(shù)據(jù)庫(kù)入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11
淺析Java中對(duì)象的創(chuàng)建與對(duì)象的數(shù)據(jù)類(lèi)型轉(zhuǎn)換
這篇文章主要介紹了Java中對(duì)象的創(chuàng)建與對(duì)象的數(shù)據(jù)類(lèi)型轉(zhuǎn)換,是Java入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-01-01
Java創(chuàng)建多線程的幾種方式實(shí)現(xiàn)
這篇文章主要介紹了Java創(chuàng)建多線程的幾種方式實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
drools規(guī)則動(dòng)態(tài)化實(shí)踐解析
這篇文章主要為大家介紹了drools規(guī)則動(dòng)態(tài)化實(shí)踐解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

