Java8 使用工廠方法supplyAsync創(chuàng)建CompletableFuture實例
目前為止我們已經(jīng)了解了如何通過編程創(chuàng)建 CompletableFuture 對象以及如何獲取返回值,雖然看起來這些操作已經(jīng)比較方便,但還有進(jìn)一步提升的空間, CompletableFuture 類自身提供了大量精巧的工廠方法,使用這些方法能更容易地完成整個流程,還不用擔(dān)心實現(xiàn)的細(xì)節(jié)。

可以看到我們使用new Thread的方式,顯然是不恰當(dāng)?shù)摹?/p>
使用工廠方法 supplyAsync創(chuàng)建 CompletableFuture
采用 supplyAsync 方法后,可以用一行代碼重寫getPriceAsync 方法。
【使用工廠方法 supplyAsync 創(chuàng)建 CompletableFuture 對象】
public Future<Double> getPriceAsync(String product) {
return CompletableFuture.supplyAsync(() -> calculatePrice(product));
}
supplyAsync 方法接受一個生產(chǎn)者( Supplier )作為參數(shù),返回一個 CompletableFuture對象,該對象完成異步執(zhí)行后會讀取調(diào)用生產(chǎn)者方法的返回值。
生產(chǎn)者方法會交由 ForkJoinPool池中的某個執(zhí)行線程( Executor )運行,但是你也可以使用 supplyAsync 方法的重載版本,傳遞第二個參數(shù)指定不同的執(zhí)行線程執(zhí)行生產(chǎn)者方法。
一般而言,向 CompletableFuture 的工廠方法傳遞可選參數(shù),指定生產(chǎn)者方法的執(zhí)行線程是可行的,后面我們會會介紹如何使用適合你應(yīng)用特性的執(zhí)行線程改善程序的性能。

對比
剛剛的代碼
public Future<Double> getPriceAsync(String product) {
return CompletableFuture.supplyAsync(() -> calculatePrice(product));
}
getPriceAsync 方法返回的 CompletableFuture 對象和 下面的代碼
public Future<Double> getPriceAsync(String product) {
CompletableFuture<Double> futurePrice = new CompletableFuture<>();
new Thread( () -> {
try {
double price = calculatePrice(product);
futurePrice.complete(price);
} catch (Exception ex) {
futurePrice.completeExceptionally(ex);
}
}).start();
return futurePrice;
}
手工創(chuàng)建和完成的 CompletableFuture 對象是完全等價的,這意味著它提供了同樣的錯誤管理機(jī)制,而前者你花費了大量的精力才得以構(gòu)建。

對CompletableFuture async的理解
驗證代碼如下
ExecutorService executorService = Executors.newFixedThreadPool(3);
//executorService.submit(new RuleTestRunnable(1));
List<Integer> taskList = new ArrayList<>();
for (int i = 0; i < 30; i++) {
taskList.add(i);
}
CompletableFuture<String> a1 = CompletableFuture.supplyAsync(() -> {
logger.info("線程1{}{}","開始");
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("線程1{}{}","結(jié)束");
return "1";
},executorService);
CompletableFuture<String> a2 = CompletableFuture.supplyAsync(() -> {
logger.info("線程2{}{}","開始");
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
logger.info("線程2{}{}","結(jié)束");
return "1";
},executorService);
CompletableFuture<Object> a= a1.thenCombineAsync(a2,(s1,s2) -> {
logger.info("組合線程{}{}");
return s1+s2;
},executorService);
Object result = a.get();
當(dāng)executorService線程池大小為2時候,執(zhí)行結(jié)果如下:
[pool-4-thread-1] INFO test.rcd.thread.CompletableFutureDemo.lambda$mains$4:127 - 組合線程{}{}
a1.thenCombineAsync方法始終被線程1或2執(zhí)行
當(dāng)executorService線程池大小為3時候,執(zhí)行結(jié)果如下:
[pool-4-thread-3] INFO test.rcd.thread.CompletableFutureDemo.lambda$mains$4:127 - 組合線程{}{}
a1.thenCombineAsync方法始終被線程3執(zhí)行
改為a1.thenCombine(),執(zhí)行結(jié)果:
a1.thenCombineAsync方法始終被線程1或2執(zhí)行
由此可見,async方法始終嘗試取新線程執(zhí)行方法,不帶async方法則會從當(dāng)前線程里取線程執(zhí)行.CompletableFuture似是與線程無關(guān)的。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- Java8?CompletableFuture?runAsync學(xué)習(xí)總結(jié)submit()?execute()等
- Java?CompletableFuture實現(xiàn)多線程異步編排
- 詳解Java8?CompletableFuture的并行處理用法
- Java8 自定義CompletableFuture的原理解析
- Java8 CompletableFuture 異步執(zhí)行操作
- Java并發(fā) CompletableFuture異步編程的實現(xiàn)
- Java8新的異步編程方式CompletableFuture實現(xiàn)
- Java8 CompletableFuture詳解
- Java中的CompletableFuture原理與用法
相關(guān)文章
Java 中的 NoSuchMethodException 異常及解決思路(最新推薦)
NoSuchMethodException異常是Java中使用反射機(jī)制時常見的錯誤,它通常由方法名或參數(shù)不匹配、訪問權(quán)限問題、方法簽名不匹配等原因引發(fā),解決方法包括核實方法名及其參數(shù)類型、確認(rèn)方法訪問權(quán)限、檢查方法簽名和重載問題、確保方法存在于正確的類中,感興趣的朋友一起看看吧2025-01-01
SpringBoot使用Mybatis-Generator配置過程詳解
這篇文章主要介紹了SpringBoot使用Mybatis-Generator配置過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02
SpringCloud?Eureka應(yīng)用全面介紹
Eureka是Netflix開發(fā)的服務(wù)發(fā)現(xiàn)框架,本身是一個基于REST的服務(wù),主要用于定位運行在AWS域中的中間層服務(wù),以達(dá)到負(fù)載均衡和中間層服務(wù)故障轉(zhuǎn)移的目的2022-09-09
Java中==符號與equals()的使用詳解(測試兩個變量是否相等)
下面小編就為大家?guī)硪黄狫ava中==符號與equals()的使用詳解(測試兩個變量是否相等)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

