日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

谷歌guava_Google Guava –期货

發(fā)布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 谷歌guava_Google Guava –期货 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

谷歌guava

這篇文章是我在Google Guava上的系列文章的延續(xù),這次涵蓋了Future。 Futures類是用于使用Future / ListenableFuture接口的靜態(tài)實用程序方法的集合。 Future是已提交給ExecutorService的異步任務(wù)(可運行或可調(diào)用)的句柄。 Future接口提供以下方法:獲取任務(wù)的結(jié)果,檢查任務(wù)是否完成或取消任務(wù)。 ListenableFuture接口擴展了Future接口,并添加了將完成偵聽器設(shè)置為在任務(wù)完成后運行的功能。 要創(chuàng)建ListenableFuture,您首先需要裝飾ExecutorService實例,如下所示:

ExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());

現(xiàn)在,所有提交的Callables / Runnables將返回一個ListenableFuture。 MoreExecutors可以在com.google.common.util.concurrent包中找到。 ListenableFutures在覆蓋以前的帖子 。 期貨中有太多方法無法有效地涵蓋在一篇文章中,所以我只涉及:鏈,轉(zhuǎn)換,allAsList和successAsList。 在整個這篇文章中,我將交替使用Future和ListenableFutures。

鏈方法返回一個ListenableFuture,其值是通過從輸入Future中獲取結(jié)果并將其作為參數(shù)應(yīng)用到Function對象來計算的, Function對象又返回另一個ListenableFuture。 讓我們看一個代碼示例并逐步執(zhí)行它:

ListenableFuture<List<String>> indexSearch = luceneSearcher.searchAsync('firstName:martin');Function<List<String>, ListenableFuture<List<Person>>> queryFunction = new Function<List<String>, ListenableFuture<List<Person>>>() {@Overridepublic ListenableFuture<List<Person>> apply(final List<String> ids) {return dataService.getPersonsByIdAsync(ids);}};ListenableFuture<List<Person>> results = Futures.chain(indexSearch, queryFunction,executorService);
  • 第1行正在使用Lucene執(zhí)行異步搜索,并將返回一個ID列表,這些ID代表存儲在數(shù)據(jù)庫中的人員記錄的主鍵。 (我創(chuàng)建了一個小索引,其中存儲在Lucene中的唯一數(shù)據(jù)是id的數(shù)據(jù),其余數(shù)據(jù)僅被索引了)。
  • 第4 – 11行正在構(gòu)建功能對象,其中apply方法將使用搜索未來的結(jié)果作為輸入。 從apply返回的將來是對dataService對象的調(diào)用的結(jié)果。
  • 第12行是從鏈調(diào)用返回的未來。 一旦輸入將來完成,將使用executorService運行該功能。
  • 為了更加清楚,這是searchAsync和getPersonsByIdAsync方法的作用。 在前面的代碼示例中,這些方法調(diào)用分別來自第2行和第8行:

    public ListenableFuture<List<String>> searchAsync(final String query) {return executorService.submit(new Callable<List<String>>() {@Overridepublic List<String> call() throws Exception {return search(query);}});}public ListenableFuture<List<Person>> getPersonsByIdAsync(final List<String> ids) {return executorService.submit(new Callable<List<Person>>() {@Overridepublic List<Person> call() throws Exception {return getPersonsById(ids);}});}

    chain方法具有兩個簽名:

  • 鏈(ListentableFuture,函數(shù))
  • 鏈(ListenableFuture,函數(shù),ExecutorService)
  • 在確定使用哪種方法時,需要考慮幾點。
    如果通過調(diào)用時間鏈完成了輸入將來,則所提供的函數(shù)將在調(diào)用線程中立即執(zhí)行。 此外,如果未提供執(zhí)行程序,則使用MoreExecutors.sameThreadExecutor。 MoreExecutors.sameThreadExecutor(顧名思義)位于ThreadPoolExecutor.CallerRunsPolicy之后,這意味著提交的任務(wù)在與執(zhí)行/提交相同的線程中運行。

    轉(zhuǎn)變

    轉(zhuǎn)換方法類似于鏈式方法,因為它以Future和Function對象作為參數(shù)。 區(qū)別在于,不返回ListenableFuture,僅返回將給定功能應(yīng)用于輸入future的結(jié)果。 考慮以下:

    List<String> ids = .... ListenableFuture<List<Map<String, String>>> dbRecords = dataService.getPersonDataByIdAsync(ids);Function<List<Map<String, String>>,List<Person>> transformDbResults = new Function<List<String>, List<Person>>() {@Overridepublic List<Person> apply(List<Map<String, String>> personMapList) {List<Person> personObjList = new ArrayList<Person>();for(Map<String,String> personDataMap : personMapList){personObjList.add(new Person(personDataMap);} return personObjList;}};ListenableFuture<List<Person>> transformedResults = Futures.transform(dbRecords, transformDbResults, executorService);
  • 第2行執(zhí)行異步數(shù)據(jù)庫查找
  • 在第4行上,正在創(chuàng)建一個函數(shù)對象,但是在第8行上,請注意返回類型為List <Person>
  • transform方法具有與chain相同的重載方法調(diào)用,但有相同的警告。

    AllAsList

    allAsList方法將采用任意數(shù)量的ListenableFutures作為變量或以Iterator <ListenableFuture>的形式。 返回一個ListenableFuture,其值是所有輸入結(jié)果的列表。 列表中返回的值與原始列表的順序相同。 如果任何輸入值被取消或失敗,則返回的ListenableFuture也將被取消或失敗。 從allAsList調(diào)用取消返回的future不會傳播到列表中提交的任何原始任務(wù)。

    ListenableFuture<List<Person>> lf1 = getPersonsByFirstNameFuture('martin'); ListenableFuture<List<Person>> lf2 = getPersonsByFirstNameFuture('bob'); ListenableFuture<List<List<Person>>> lfResults = Futures.allAsList(lf1, lf2); //assume lf1 failed List<List<Person>> personLists = lfResults.get() //call results in exception


    成功名單

    successAsList方法與allAsList非常相似,但是更加寬容。 就像allAsList一樣,successAsList返回結(jié)果列表的順序與輸入列表的順序相同,但是如果任何輸入失敗或被取消,則列表中的相應(yīng)值將為null。 取消返回的將來也不會取消任何原始輸入。

    ListenableFuture<List<Person>> lf1 = getPersonsByFirstNameFuture('martin'); ListenableFuture<List<Person>> lf2 = getPersonsByFirstNameFuture('bob'); ListenableFuture<List<List<Person>>> lfResults = Futures.successfulAsList(lf1, lf2); //assume lf1 failed List<List<Person>> personLists = lfResults.get(); List<Person> listOne = personLists.get(0) //listOne is null List<Person> listTwo = personLists.get(1) //listTwo, not null


    結(jié)論

    希望這有助于從Google Guava發(fā)現(xiàn)Futures類中包含的有用性。 我創(chuàng)建了一個單元測試,以顯示本文中描述的方法的示例用法。 由于有大量支持代碼,因此我在gihub上創(chuàng)建了一個項目guava-blog 。 該項目還將包含我以前在Guava上的帖子( Monitor , ListenableFuture )中的源代碼。 一如既往地歡迎提出意見和建議。

    資源資源

    • 番石榴項目首頁
    • 期貨API
    • 博客系列的源代碼


    參考資料: Google Guava –我們的JCG合作伙伴 Bill Bejeck撰寫的期貨,來自Random Thoughts On Coding博客。

    翻譯自: https://www.javacodegeeks.com/2012/11/google-guava-futures.html

    谷歌guava

    總結(jié)

    以上是生活随笔為你收集整理的谷歌guava_Google Guava –期货的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。