Guava為Java并行編程Future提供了很多有用擴(kuò)展,其主要接口為L(zhǎng)istenableFuture,并借助于Futures靜態(tài)擴(kuò)展。
繼承至Future的ListenableFuture,允許我們添加回調(diào)函數(shù)在線(xiàn)程運(yùn)算完成時(shí)返回值或者方法執(zhí)行完成立即返回。
對(duì)ListenableFuture添加回調(diào)函數(shù):
Futures.addCallback(ListenableFuture<V>, FutureCallback<V>, Executor)
其中 FutureCallback是一個(gè)包含onSuccess(V),onFailure(Throwable)的接口。
使用如:
1
2
3
4
5
6
7
8
9
10
|
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()); } }); |
同時(shí)Guava中Futures對(duì)于Future擴(kuò)展還有:
- transform:對(duì)于ListenableFuture的返回值進(jìn)行轉(zhuǎn)換。
- allAsList:對(duì)多個(gè)ListenableFuture的合并,返回一個(gè)當(dāng)所有Future成功時(shí)返回多個(gè)Future返回值組成的List對(duì)象。注:當(dāng)其中一個(gè)Future失敗或者取消的時(shí)候,將會(huì)進(jìn)入失敗或者取消。
- successfulAsList:和allAsList相似,唯一差別是對(duì)于失敗或取消的Future返回值用null代替。不會(huì)進(jìn)入失敗或者取消流程。
- immediateFuture/immediateCancelledFuture: 立即返回一個(gè)待返回值的ListenableFuture。
- makeChecked: 將ListenableFuture 轉(zhuǎn)換成CheckedFuture。CheckedFuture 是一個(gè)ListenableFuture ,其中包含了多個(gè)版本的get 方法,方法聲明拋出檢查異常.這樣使得創(chuàng)建一個(gè)在執(zhí)行邏輯中可以?huà)伋霎惓5腇uture更加容易
- JdkFutureAdapters.listenInPoolThread(future): guava同時(shí)提供了將JDK Future轉(zhuǎn)換為L(zhǎng)istenableFuture的接口函數(shù)。
下邊是一個(gè)對(duì)于Future的測(cè)試demo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
@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()); } |
官方資料主頁(yè):https://awk.so/@code.google.com!/p/guava-libraries/wiki/ListenableFutureExplained
以上就是對(duì)Guava - 并行編程Futures 的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料謝謝大家對(duì)本站的支持!