国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Guava - 并行編程Futures詳解

2019-11-26 13:50:11
字體:
來源:轉載
供稿:網友

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

繼承至Future的ListenableFuture,允許我們添加回調函數在線程運算完成時返回值或者方法執行完成立即返回。

對ListenableFuture添加回調函數:

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擴展還有:

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

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

@Testpublic 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 的資料整理,后續繼續補充相關資料謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 耿马| 杭锦后旗| 桐柏县| 乐清市| 四子王旗| 黔南| 浦北县| 濮阳市| 万源市| 长垣县| 自治县| 聂荣县| 抚松县| 苍南县| 三河市| 宝鸡市| 梁平县| 固镇县| 和平区| 南开区| 巢湖市| 互助| 通化市| 宣汉县| 普格县| 丹棱县| 固始县| 安化县| 贵州省| 眉山市| 犍为县| 遂溪县| 开远市| 宜良县| 南城县| 平泉县| 双鸭山市| 隆子县| 吉木乃县| 金堂县| 綦江县|