Java8 使用工廠方法supplyAsync創(chuàng)建CompletableFuture實(shí)例
目前為止我們已經(jīng)了解了如何通過編程創(chuàng)建 CompletableFuture 對(duì)象以及如何獲取返回值,雖然看起來這些操作已經(jīng)比較方便,但還有進(jìn)一步提升的空間, CompletableFuture 類自身提供了大量精巧的工廠方法,使用這些方法能更容易地完成整個(gè)流程,還不用擔(dān)心實(shí)現(xiàn)的細(xì)節(jié)。
可以看到我們使用new Thread的方式,顯然是不恰當(dāng)?shù)摹?/p>
使用工廠方法 supplyAsync創(chuàng)建 CompletableFuture
采用 supplyAsync 方法后,可以用一行代碼重寫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{}{}","開始"); 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時(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í)行
由此可見,async方法始終嘗試取新線程執(zhí)行方法,不帶async方法則會(huì)從當(dāng)前線程里取線程執(zhí)行.CompletableFuture似是與線程無關(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)文章
springmvc實(shí)現(xiàn)跨服務(wù)器文件上傳功能
這篇文章主要為大家詳細(xì)介紹了springmvc實(shí)現(xiàn)跨服務(wù)器文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08SpringBoot整合Shiro實(shí)現(xiàn)權(quán)限控制的代碼實(shí)現(xiàn)
Apache Shiro是一個(gè)強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼和會(huì)話管理,今天通過本文給大家介紹SpringBoot整合Shiro實(shí)現(xiàn)權(quán)限控制的方法,感興趣的朋友一起看看吧2021-07-07Springboot支持Emoji表情的實(shí)現(xiàn)方法
本文主要介紹了Springboot 支持Emoji 表情,本篇的實(shí)現(xiàn)方式是僅需后端處理,具有一定的參考價(jià)值,需要的朋友可以參考一下。2021-07-07SpringMVC的處理器攔截器HandlerInterceptor詳解
這篇文章主要介紹了SpringMVC的處理器攔截器HandlerInterceptor詳解,SpringWebMVC的處理器攔截器,類似于Servlet開發(fā)中的過濾器Filter,用于處理器進(jìn)行預(yù)處理和后處理,需要的朋友可以參考下2024-01-01java線程之使用Runnable接口創(chuàng)建線程的方法
本篇文章介紹了,java中使用Runnable接口創(chuàng)建線程的方法。需要的朋友參考下2013-05-05如何在Spring?Boot框架中使用攔截器實(shí)現(xiàn)URL限制
在Spring?Boot框架中,您可以使用攔截器(Interceptor)來控制限制URL列表,本文通過一個(gè)簡(jiǎn)單的示例給大家介紹Spring?Boot?攔截器實(shí)現(xiàn)URL限制的操作方法,感興趣的朋友跟隨小編一起看看吧2023-08-08