AsyncFuntion接口与之前学习吃的使用Function和Functions进行对象转换有很密切的联系,AsyncFuction接口是Function接口的异步表现,AsyncFuction和Function都需要接收一个input参数,不同的是AsyncFunction接口返回的是 ListenableFuture,当我们需要接收AsyncFunction转换后的结果时,我们需要调用 ListenableFuture.get()方法。

AsyncFunction接口常被用于当我们想要异步的执行转换而不造成线程阻塞时,尽管Future.get()方法会在任务没有完成时造成阻塞,但 是AsyncFunction接口并不被建议用来异步的执行转换,它常被用于返回Future实例。

import java.util.concurrent.Callable;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.Executors;

import com.google.common.util.concurrent.AsyncFunction;

import com.google.common.util.concurrent.Futures;

import com.google.common.util.concurrent.ListenableFuture;

import com.google.common.util.concurrent.ListeningExecutorService;

import com.google.common.util.concurrent.MoreExecutors;

public class AsyncFunctionTest {

private static ListeningExecutorService listeningExecutorService =

MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2));

static AsyncFunction<String, String> asyncFunction = new AsyncFunction<String, String>() {

@Override

public ListenableFuture<String> apply(final String input) throws Exception {

return listeningExecutorService.submit(new Callable<String>() {

@Override

public String call() throws Exception {

return input + "101";

}

});

}

};

static ListenableFuture<String> lis = listeningExecutorService.submit(new Callable<String>() {

@Override

public String call() throws Exception {

return "张三";

}

});

public static void main(String[] args) throws InterruptedException, ExecutionException {

ListenableFuture<String> lf =

Futures.transform(lis, asyncFunction);

System.out.println(lf.get());

listeningExecutorService.shutdown();

}

}

guava学习--AsyncFunction的更多相关文章

  1. Guava学习笔记目录

    Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...

  2. guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁

    guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...

  3. guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...

  4. Guava学习

    Guava学习笔记目录 Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concu ...

  5. [置顶] Guava学习之ArrayListMultimap

    ArrayListMultimap类的继承关系如下图所示: Guava ArrayListMultimap List Multimap 是一个接口,继承自 Multimap 接口.ListMultim ...

  6. [置顶] Guava学习之Splitter

    Splitter:在Guava官方的解释为:Extracts non-overlapping substrings from an input string, typically by recogni ...

  7. [置顶] Guava学习之Iterators

    Iterators类提供了返回Iterator类型的对象或者对Iterator类型对象操作的方法.除了特别的说明,Iterators类中所有的方法都在Iterables类中有相应的基于Iterable ...

  8. [置顶] Guava学习之Lists

    Lists类主要提供了对List类的子类构造以及操作的静态方法.在Lists类中支持构造ArrayList.LinkedList以及newCopyOnWriteArrayList对象的方法.其中提供了 ...

  9. [置顶] Guava学习之Immutable集合

    Immutable中文意思就是不可变.那为什么需要构建一个不可变的对象?原因有以下几点: 在并发程序中,使用Immutable既保证线程安全性,也大大增强了并发时的效率(跟并发锁方式相比).尤其当一个 ...

随机推荐

  1. js阻止form表单重复提交

    防止表单重复提交的方法总体来说有两种,一种是在js中阻止重复提交:另一种是在后台利用token令牌实现,大致思路是生成一个随机码放到session和form表单的隐藏输入框中,提交表单时两者对比,表单 ...

  2. js中arguments的用法

    了解这个对象之前先来认识一下javascript的一些功能: 其实Javascript并没有重载函数的功能,但是Arguments对象能够模拟重载.Javascrip中国每个函数都会有一个Argume ...

  3. Angular--页面间切换及传值的四种方法

    1. 基于ui-router的页面跳转传参(1) 在AngularJS的app.js中用ui-router定义路由,比如现在有两个页面,一个页面(producers.html)放置了多个produce ...

  4. PHP三元运算符 isset($_GET['id']) ? $_GET['id'] : ”

    $id = isset($_GET['id']) ? $_GET['id'] : ''; ?> 这是一个php的三元运算符 (条件) ? (值1):(值2); 解释:如果条件成立(为真),则执行 ...

  5. 定时备份SQL SERVER的数据库并且把备份文件复制到另外一台服务器

    需求介绍:每天备份线上正式库并且把备份文件复制到测试服务器,测试服务器自动把数据库备份文件还原. 方案介绍: 第1步:在正式库上创建存储过程用来备份数据库和复制到测试服务器,然后新建作业每天定时执行创 ...

  6. 配置L2TP IPsec VPN (CentOS 6.5)

    1. 安装相关包 yum install -y ppp iptables make gcc gmp-devel xmlto bison flex libpcap-devel lsof vim-enha ...

  7. VM出现该虚拟机正在使用中的提示,让获取所有权限解决办法

    今天打开虚拟机正要学习,结果说是虚拟机似乎正在使用中,让我重新获取权限.解决办法:打开提示的配置文件的位置,将一个以.lck结尾的文件夹删除或者保存为另外的文件名称,再打开虚拟机就OK了.

  8. Android runtime Exception 整理

    一般面试中java Exception(runtimeException )是必会被问到的问题 常见的异常列出四五种,是基本要求.更多的....需要注意积累了   常见的几种如下:   NullPoi ...

  9. 用户识别APP项目开发计划书

    用户识别APP项目开发计划书        项目介绍: 用户识别APP,通过在有限时间内引导用户A交互,提取用户的行为特征,然后将APP交给用户X(可能是A也可能是陌生人),在1分钟内引导X交互,判断 ...

  10. Failed to load resource: the server responded with a status of 500 (Internal Server Error)

    错误提示: 原因: MIME类型错误. 之前添加json.woff.woff2映射,更换系统(Win7升Win10)后配置失效,在webconfig中删除映射即可,因为Win10自带上面3个MIME映 ...