欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Guava - 并行編程Futures詳解

 更新時間:2016年09月15日 09:24:07   作者:破狼  
這篇文章主要介紹了Guava - 并行編程Futures詳解方法的相關(guān)資料,需要的朋友可以參考下

Guava為Java并行編程Future提供了很多有用擴(kuò)展,其主要接口為ListenableFuture,并借助于Futures靜態(tài)擴(kuò)展。

繼承至Future的ListenableFuture,允許我們添加回調(diào)函數(shù)在線程運(yùn)算完成時返回值或者方法執(zhí)行完成立即返回。

對ListenableFuture添加回調(diào)函數(shù):

Futures.addCallback(ListenableFuture<V>, FutureCallback<V>, Executor)

其中 FutureCallback是一個包含onSuccess(V),onFailure(Throwable)的接口。

使用如:

Futures.addCallback(ListenableFuture, new FutureCallback<Object>() {

  public void onSuccess(Object result) {
    System.out.printf("onSuccess with: %s%n", result);
  }

  public void onFailure(Throwable thrown) {
    System.out.printf("onFailure %s%n", thrown.getMessage());
  }
});

同時Guava中Futures對于Future擴(kuò)展還有:

  1. transform:對于ListenableFuture的返回值進(jìn)行轉(zhuǎn)換。
  2. allAsList:對多個ListenableFuture的合并,返回一個當(dāng)所有Future成功時返回多個Future返回值組成的List對象。注:當(dāng)其中一個Future失敗或者取消的時候,將會進(jìn)入失敗或者取消。
  3. successfulAsList:和allAsList相似,唯一差別是對于失敗或取消的Future返回值用null代替。不會進(jìn)入失敗或者取消流程。
  4. immediateFuture/immediateCancelledFuture: 立即返回一個待返回值的ListenableFuture。
  5. makeChecked: 將ListenableFuture 轉(zhuǎn)換成CheckedFuture。CheckedFuture 是一個ListenableFuture ,其中包含了多個版本的get 方法,方法聲明拋出檢查異常.這樣使得創(chuàng)建一個在執(zhí)行邏輯中可以拋出異常的Future更加容易
  6. JdkFutureAdapters.listenInPoolThread(future): guava同時提供了將JDK Future轉(zhuǎn)換為ListenableFuture的接口函數(shù)。

下邊是一個對于Future的測試demo:

@Test
public void should_test_furture() throws Exception {
  ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));

  ListenableFuture future1 = service.submit(new Callable<Integer>() {
    public Integer call() throws InterruptedException {
      Thread.sleep(1000);
      System.out.println("call future 1.");
      return 1;
    }
  });

  ListenableFuture future2 = service.submit(new Callable<Integer>() {
    public Integer call() throws InterruptedException {
      Thread.sleep(1000);
      System.out.println("call future 2.");
  //    throw new RuntimeException("----call future 2.");
      return 2;
    }
  });

  final ListenableFuture allFutures = Futures.allAsList(future1, future2);

  final ListenableFuture transform = Futures.transform(allFutures, new AsyncFunction<List<Integer>, Boolean>() {
    @Override
    public ListenableFuture apply(List<Integer> results) throws Exception {
      return Futures.immediateFuture(String.format("success future:%d", results.size()));
    }
  });

  Futures.addCallback(transform, new FutureCallback<Object>() {

    public void onSuccess(Object result) {
      System.out.println(result.getClass());
      System.out.printf("success with: %s%n", result);
    }

    public void onFailure(Throwable thrown) {
      System.out.printf("onFailure%s%n", thrown.getMessage());
    }
  });

  System.out.println(transform.get());
}

官方資料主頁:https://awk.so/@code.google.com!/p/guava-libraries/wiki/ListenableFutureExplained

以上就是對Guava - 并行編程Futures 的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料謝謝大家對本站的支持!

相關(guān)文章

  • SpringBoot @RequestParam、@PathVaribale、@RequestBody實(shí)戰(zhàn)案例

    SpringBoot @RequestParam、@PathVaribale、@RequestBody實(shí)戰(zhàn)案例

    這篇文章主要介紹了SpringBoot @RequestParam、@PathVaribale、@RequestBody實(shí)戰(zhàn)案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • kotlin之閉包案例詳解

    kotlin之閉包案例詳解

    這篇文章主要介紹了kotlin之閉包案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 啟動 Eclipse 彈出 Failed to load the JNI shared library jvm.dll 錯誤的解決方法

    啟動 Eclipse 彈出 Failed to load the JNI shared library jvm.dll

    這篇文章主要介紹了有時候,新電腦上回碰到打開Eclipse時,彈出提示“Failed to load the JNI shared library jvm.dll”錯誤,這里給大家分享解決方案
    2016-08-08
  • 使用java8的方法引用替換硬編碼的示例代碼

    使用java8的方法引用替換硬編碼的示例代碼

    這篇文章主要介紹了使用java8的方法引用替換硬編碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • 將Java程序的輸出結(jié)果寫到txt文件中的方法

    將Java程序的輸出結(jié)果寫到txt文件中的方法

    今天小編就為大家分享一篇將Java程序的輸出結(jié)果寫到txt文件中的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • IDEA快速生成實(shí)體類的示例教程

    IDEA快速生成實(shí)體類的示例教程

    這篇文章主要介紹了IDEA快速生成實(shí)體類的示例教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Java實(shí)現(xiàn)把兩個數(shù)組合并為一個的方法總結(jié)

    Java實(shí)現(xiàn)把兩個數(shù)組合并為一個的方法總結(jié)

    這篇文章主要介紹了Java實(shí)現(xiàn)把兩個數(shù)組合并為一個的方法,結(jié)合實(shí)例形式總結(jié)分析了java常用的四種數(shù)組合并操作技巧,需要的朋友可以參考下
    2017-12-12
  • Spring Boot 注解方式自定義Endpoint詳解

    Spring Boot 注解方式自定義Endpoint詳解

    這篇文章主要介紹了Spring Boot注解方式自定義Endpoint詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring MVC Mybatis多數(shù)據(jù)源的使用實(shí)例解析

    Spring MVC Mybatis多數(shù)據(jù)源的使用實(shí)例解析

    項(xiàng)目需要從其他網(wǎng)站獲取數(shù)據(jù),因?yàn)槭桥R時加的需求,這篇文章主要介紹了Spring MVC Mybatis多數(shù)據(jù)源的使用實(shí)例解析,需要的朋友可以參考下
    2016-12-12
  • Spring中@RestControllerAdvice注解的使用詳解

    Spring中@RestControllerAdvice注解的使用詳解

    這篇文章主要介紹了Spring中@RestControllerAdvice注解的使用詳解,@RestControllerAdvice是一個組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,需要的朋友可以參考下
    2024-01-01

最新評論