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. Windows7+32位,MongoDB安装

    在网上找了各种安装MongoDB的教程,总是会出现一些bug,就自己总结了,亲测正确,MongoDB的安装再也不是一件麻烦的事情了~ 1.下载好跟自己电脑适合的安装包,选择Custom自定义安装,将安 ...

  2. Xamarin.Forms listview中的button按钮,实现带着参数返回上一级页面

    今天在做列表显示的时候遇到一个问题,就是在ListView中如何才能让一个button的按钮工作并且包含参数呢? 其实有点类似于rep里的控件无法起获取一样.在Xamarin中,当你button绑定事 ...

  3. Unbuntu下使用SVN

    一.查看SVN版本 svn --version 二.查看SVN仓库 # 查看被svn仓库占用的所有端口 netstat -anp |grep svn # 查看所有端口对应的仓库 ps -ef | gr ...

  4. python 中的decorator

    python 中decorator的作用就是一个包装的作用,所谓包装指在执行真正的函数之前或者之后,我们可以有一些额外的发挥余地. decorator形式如下 def dec(arg1): print ...

  5. 简介python2.x的编码

    python2.x的中文编码真是令人头痛,简单写下自己的一点python编码转换的体会. windows平台用的默认编码格式为gbk >>> s = raw_input() #在wi ...

  6. cocoapods使用一直Updating local specs repositories的解决方案

    pod install 换成pod install --verbose --no-repo-update这个命令,前面的命令被墙了 ,<pre id="best-content-226 ...

  7. 渗透杂记-2013-07-13 关于SMB版本的扫描

    smb2的溢出,其实在metasploit里面有两个扫描器可以用,效果都差不多,只是一个判断的更加详细,一个只是粗略的判断. Welcome to the Metasploit Web Console ...

  8. javax.crypto.BadPaddingException: Given final block not properly padded

    一.报错 写了一个加密方法,在Windows上运行没有问题,在Linux上运行时提示如下错误: javax.crypto.BadPaddingException: Given final block ...

  9. Indexed 和 Stored 的区别

    都是针对field的: indexed表示需不需要建立索引,以便之后对这个field进行查询:        stored表示需不需要随索引同时存储这个field本身的内容,以便查询时直接从结果中获取 ...

  10. 安装配置dradis

    github:https://github.com/dradis/dradis-ce/blob/master/README.md安装出现错误:== Copying sample files == == ...