Guava
来源:互联网 发布:国家基础教育网络平台 编辑:程序博客网 时间:2024/05/14 15:50
这里转载一篇关于Guava - 并行编程Futures介绍,随后附上自己的调用:
1、介绍
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扩展还有:
transform:对于ListenableFuture的返回值进行转换。
allAsList:对多个ListenableFuture的合并,返回一个当所有Future成功时返回多个Future返回值组成的List对象。注:当其中一个Future失败或者取消的时候,将会进入失败或者取消。
successfulAsList:和allAsList相似,唯一差别是对于失败或取消的Future返回值用null代替。不会进入失败或者取消流程。
immediateFuture/immediateCancelledFuture: 立即返回一个待返回值的ListenableFuture。
makeChecked: 将ListenableFuture 转换成CheckedFuture。CheckedFuture 是一个ListenableFuture ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易
JdkFutureAdapters.listenInPoolThread(future): guava同时提供了将JDK Future转换为ListenableFuture的接口函数。
2、下边是一个对于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());
}
运行结果:
call future 1.
call future 2.
class java.lang.String
success with: success future number:2
success future number:2
3、自己调用的devopts代码实例
ListenableFuture<Response> response = asyncExecuteHttp(request);
// Futures.
return Futures.transform(response, new AsyncFunction<Response, JsonResponse>() {
public ListenableFuture<JsonResponse> apply(Response httpResponse) throws Exception {
JsonResponse json = extractJsonResponse(httpResponse, expectedHttpStatusCodes);
return Futures.immediateFuture(json);
}
});
- Guava
- Guava
- Guava
- GUAVA
- guava
- guava
- Guava
- Guava
- guava
- Guava
- Guava
- Guava
- Guava
- Guava
- guava
- guava
- guava
- Guava
- Java学习笔记10——Servlet
- NKOJ-1517 选路
- 交换两个变量的值(三种方法)
- mysql 在select * from A left join B 多张表,不同表的主建ID会发生冲突的解决办法
- 二叉树知识点
- Guava
- 802.11无线网络权威——(一二、导论)
- 【raspberry】(五) 四位数码管F3461BH
- 算法导论第15章练习题 15.4-4
- 胖五加油!作为程序员_该如何去挽救一个失败的项目?
- 记录
- 《大话设计模式》——读后感 (8)好菜每回味不同——建造者模式之经典案例(2)
- C#给软件加注册码
- Python之MD5加密