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. android中webrtc的几个关键的状态

    在android层使用webrtc的时候,都是通过native层回调的形式来触发ui的改变,比如在什么时候绘出对方的视频窗口,什么时候表示双方连接已经建立等等... 我现在把我知道的列出来用于备忘. ...

  2. (4) 二叉平衡树, AVL树

    1.为什么要有平衡二叉树? 上一节我们讲了一般的二叉查找树, 其期望深度为O(log2n), 其各操作的时间复杂度O(log2n)同时也是由此决定的.但是在某些情况下(如在插入的序列是有序的时候), ...

  3. .net平台下获取高精度时间类

    原文:http://www.it118.org/specials/321869dd-98cb-431b-b6d2-82d973cd739d/d940a15c-0d36-4369-8de0-b07cf3 ...

  4. 参考:鼠标点击事件(Selenium)

    1.鼠标右键点击操作:Actions action = new Actions(driver) ;action.contextClick(driver.findElement(By.xpath(xpa ...

  5. TFS二次开发系列:二、TFS的安装

    本系列的实例将采用TFS 2012+Sql Server2012编写. TFS的完整版本安装最好是在Windows server2008 64位以上版本中,其包括64位的SQL SERVER 2012 ...

  6. Openfire用户密码加密解密

    需求要求审核过程中都用匿名进行用户注册登录,注册用户审核通过后才使用openfire内置表 如何做到用户密码统一 Openfire是通过org.jivesoftware.util.Blowfish.j ...

  7. HackPwn2015:IoT智能硬件安全威胁分析

    HackPwn2015:IoT智能硬件安全威胁分析 360安全卫士 · 2015/08/26 14:43 IoT(物联网)是一种既危险又有趣的混合技术,所谓的混合技术包括移动应用程序.蓝牙.Wi-Fi ...

  8. 个人博客作业Week2

    一.是否需要有代码规范 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西. 我反驳这个观点,这些规范是成千上万的程序员在开发程序中总结出来的代码规范,他有助于我们的开 ...

  9. Asp.Net完美隐藏服务器信息

    首先在Global.asax.cs里增加: protected void Application_PreSendRequestContent(object sender, EventArgs e){H ...

  10. eclipse设置项目编码

    首先Windows->Preferences, 然后选择General下面的Workspace. Text file encoding选择Other GBK, 如果没有GBK的选项, 没关系, ...