Futures JAVA,Guava - 并行编程Futures详解

Guava为Java并行编程Future提供了很多有用扩展,其主要接口为ListenableFuture,并借助于Futures静态扩展。

继承至Future的ListenableFuture,允许我们添加回调函数在线程运算完成时返回值或者方法执行完成立即返回。

对ListenableFuture添加回调函数:

Futures.addCallback(ListenableFuture, FutureCallback, Executor)

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

使用如:

Futures.addCallback(ListenableFuture, new FutureCallback() {

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的接口函数。

下边是一个对于Future的测试demo:

@Test

public void should_test_furture() throws Exception {

ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));

ListenableFuture future1 = service.submit(new Callable() {

public Integer call() throws InterruptedException {

Thread.sleep(1000);

System.out.println("call future 1.");

return 1;

}

});

ListenableFuture future2 = service.submit(new Callable() {

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, Boolean>() {

@Override

public ListenableFuture apply(List results) throws Exception {

return Futures.immediateFuture(String.format("success future:%d", results.size()));

}

});

Futures.addCallback(transform, new FutureCallback() {

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());

}

以上就是对Guava - 并行编程Futures 的资料整理,后续继续补充相关资料谢谢大家对本站的支持!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值