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

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

對(duì)比
剛剛的代碼
public Future<Double> getPriceAsync(String product) {
return CompletableFuture.supplyAsync(() -> calculatePrice(product));
}
getPriceAsync 方法返回的 CompletableFuture 對(duì)象和 下面的代碼
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 對(duì)象是完全等價(jià)的,這意味著它提供了同樣的錯(cuò)誤管理機(jī)制,而前者你花費(fèi)了大量的精力才得以構(gòu)建。

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

