Guava - 并行編程Futures詳解
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ò)展還有:
- transform:對于ListenableFuture的返回值進(jìn)行轉(zhuǎn)換。
- allAsList:對多個ListenableFuture的合并,返回一個當(dāng)所有Future成功時返回多個Future返回值組成的List對象。注:當(dāng)其中一個Future失敗或者取消的時候,將會進(jìn)入失敗或者取消。
- successfulAsList:和allAsList相似,唯一差別是對于失敗或取消的Future返回值用null代替。不會進(jìn)入失敗或者取消流程。
- immediateFuture/immediateCancelledFuture: 立即返回一個待返回值的ListenableFuture。
- makeChecked: 將ListenableFuture 轉(zhuǎn)換成CheckedFuture。CheckedFuture 是一個ListenableFuture ,其中包含了多個版本的get 方法,方法聲明拋出檢查異常.這樣使得創(chuàng)建一個在執(zhí)行邏輯中可以拋出異常的Future更加容易
- 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)資料謝謝大家對本站的支持!
- Java基于Guava Retrying實(shí)現(xiàn)重試功能
- Springboot整合GuavaCache緩存過程解析
- Java內(nèi)存緩存工具Guava LoadingCache使用解析
- SpringBoot加入Guava Cache實(shí)現(xiàn)本地緩存代碼實(shí)例
- 詳解Guava Cache本地緩存在Spring Boot應(yīng)用中的實(shí)踐
- springboot使用GuavaCache做簡單緩存處理的方法
- RateLimit-使用guava來做接口限流代碼示例
- Java編程guava RateLimiter實(shí)例解析
- Java Guava排序器Ordering原理及代碼實(shí)例
相關(guān)文章
SpringBoot Security安裝配置及Thymeleaf整合
這篇文章主要介紹了SpringBoot Security安裝配置及Thymeleaf整合,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12
Java 基于tcp協(xié)議實(shí)現(xiàn)文件上傳
這篇文章主要介紹了Java 基于tcp協(xié)議實(shí)現(xiàn)文件上傳,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2020-11-11
詳解Java并發(fā)編程中的優(yōu)先級隊(duì)列PriorityBlockingQueue
PriorityBlockingQueue是Java中實(shí)現(xiàn)了堆數(shù)據(jù)結(jié)構(gòu)的線程安全的有界阻塞隊(duì)列。本文將會深入解讀PriorityBlockingQueue的源碼實(shí)現(xiàn),感興趣的可以了解一下2023-05-05
Java基礎(chǔ)學(xué)習(xí)之標(biāo)簽
在Java中,標(biāo)簽必須在循環(huán)之前使用, 一個循環(huán)之中嵌套另一個循環(huán)的開關(guān),從多重嵌套中continue或break,該文詳細(xì)介紹了標(biāo)簽的相關(guān)知識,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們還很有幫助,需要的朋友可以參考下2021-05-05
詳解Java如何實(shí)現(xiàn)基于Redis的分布式鎖
在不同進(jìn)程需要互斥地訪問共享資源時,分布式鎖是一種非常有用的技術(shù)手段。這篇文章運(yùn)用圖文和實(shí)例代碼介紹了Java如何實(shí)現(xiàn)基于Redis的分布式鎖,文章介紹的很詳細(xì),對Java和Redis剛興趣的朋友們可以參考借鑒,下面來一起看看。2016-08-08
SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡單使用方法
這篇文章主要介紹了SpringBoot2.x入門教程之引入jdbc模塊與JdbcTemplate簡單使用方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

