guava学习--asyncfunction

反光的小鱼儿 反光的小鱼儿     2022-08-20     232

关键词:

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

 

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

 

import java.util.HashMap;

import java.util.Map;

import java.util.concurrent.Callable;

import java.util.concurrent.ConcurrentMap;

import java.util.concurrent.Executors;

 

import com.google.common.collect.Maps;

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

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

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

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

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

 

public class AsyncFunctionSample implements AsyncFunction<Long, String> {

private ConcurrentMap<Long, String> map = Maps.newConcurrentMap();

private ListeningExecutorService listeningExecutorService =

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

// 这里简单的模拟一个service

private Map<Long, String> service = new HashMap<Long, String>() {

{

put(1L, "retrieved");

}

};

 

@Override

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

if (map.containsKey(input)) {

SettableFuture<String> listenableFuture = SettableFuture.create();

listenableFuture.set(map.get(input));

return listenableFuture;

} else {

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

@Override

public String call() throws Exception {

// service中通过input获取retrieved

String retrieved = service.get(input);

map.putIfAbsent(input, retrieved);

return retrieved;

}

});

}

}

 

public static void main(String[] args) throws Exception {

AsyncFunctionSample afs = new AsyncFunctionSample() {

@Override

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

return super.apply(input);

}

};

System.out.println(afs.apply(1L).get());

}

}

guava库学习:学习concurrencysettablefuture(代码片段)

转自:https://my.oschina.net/realfighter/blog/349931链接地址:http://www.xx566.com/detail/160.html  上一篇,Guava库学习:学习Concurrency(四)FutureCallback中,FutureCallback接口提供了onSuccess和onFailure方法,用于接收任务执行的结果, 查看详情

guava学习--事件驱动模型

转载:http://www.cnblogs.com/whitewolf/p/4132840.html     http://www.cnblogs.com/peida/p/EventBus.html 更好的文章:https://my.oschina.net/realfighter/blog/406342 Guava在guava-libraries中 查看详情

guava学习:guava集合工具-table接口(代码片段)

最近学习了下guava的使用,这里简单记录下一些常用并且使用的工具类把。看到table的使用时候真的是眼前一亮,之前的代码中写过很多的Map<String,Map<String,String>>这种格式的代码,这种阅读起来非常的不友好,甚至都不... 查看详情

guava学习

一、Stringspackagecom.google.common.base;Srings类程序中经常使用。比如判断字符串是否为空,我们在之前用jdk方法判断是会用下面这个判断语句。if(input==null||input.equals("")){System.out.println("输入字符串为空");} 上面的代码如果不注意的... 查看详情

guava源码学习ordering

基于版本:Guava22.0Wiki:Ordering 0.Ordering简介Guava的Ordering提供了链式风格的比较器的实现,我们可以用Ordering轻松构建复杂的比较器。 1.类图这张类图不完全,实际上Ordering有十几个子类,这些子类共同提供了复杂的功能。&... 查看详情

guava学习--functionpredicate

Function用于同步转换。Predicate用于过滤。 importjava.util.Collection;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importcom.google.common.base.Function;importcom.google.common.base.Funct 查看详情

guava学习--settablefuture

转载:https://my.oschina.net/realfighter/blog/349931翻开SettableFuture的源码,我们看到SettableFuture继承了AbstractFuture抽象类,AbstractFuture抽象类实现了ListenableFuture接口,所以SettableFuture类也是ListenableFuture接口的一种实现,源码相当的简单,... 查看详情

guava学习--集合2

Table:当我们需要多个索引的数据结构的时候,通常情况下,我们只能用这种丑陋的Map<FirstName,Map<LastName,Person>>来实现。为此Guava提供了一个新的集合类型-Table集合类型,来支持这种数据结构的使用场景。Table支持“row... 查看详情

guava源码学习eventbus

基于版本:Guava22.0Wiki:EventBus 0.EventBus简介提供了发布-订阅模型,可以方便的在EventBus上注册订阅者,发布者可以简单的将事件传递给EventBus,EventBus会自动将事件传递给相关联的订阅者。支持同步/异步模式。只能用于线程间... 查看详情

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

1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code-with-guava-optionals-and-preconditions/,有说的不对的地方,欢迎斧正。2,我开发软件的时候,朝着干净代码发展是我的特权,有一段时间,我曾在我几乎所有的项目中使用谷... 查看详情

guava学习--集合1

Lists:其内部使用了静态工厂方法代替构造器,提供了许多用于List子类构造和操作的静态方法,我们简单的依次进行说明,如下:newArrayList():构造一个可变的、空的ArrayList实例。newArrayList(E...elements):构造一个可变的包含传入元... 查看详情

guava学习--hashing

128位的MurmurHash(烽火使用过):看一下Java标准库中的非加密哈希算法你会发现少了MurmurHash,这是一个简单高效且还是分布式的算法,在许多语言中都有着很好的支持。我们并不是说要用它来取代Java的hashCode方法,不过如果你想要... 查看详情

关于guava集合的一些学习笔记

publicclassSetGuava{ publicstaticvoidmain(String[]args){ /** *GuavaAPI提供了有用的新的集合类型,协同已经存在的java集合工作的很好。分别是Multimap,Multiset, *Table,BiMap,ClassToInstanceMap// */// System.out.println("Multimap:一种k 查看详情

guava学习笔记:optional优雅的使用null

在我们学习和使用Guava的Optional之前,我们需要来了解一下Java中null。因为,只有我们深入的了解了null的相关知识,我们才能更加深入体会领悟到Guava的Optional设计和使用上的优雅和简单。   null代表不确定的对象:  Jav... 查看详情

guava学习--file

使用Files类来执行那些基本的任务,比如:移动或复制文件,或读取文件内容到一个字符串集合Closer类,提供了一种非常干净的方式,确保Closeable实例被正确的关闭ByteSource和CharSource类,提供了不可变的输入流(Input)和读(Reader... 查看详情

guava学习--objects

转载:https://my.oschina.net/realfighter/blog/349821  Java中的Object类是所有Java类的超类(也就是祖先),所有对象都实现Object类中的方法,在日常的工作中,我们经常需要重写其中的几个方法,如:equals、toString、hashCode等方法,... 查看详情

guava学习--suppliersuppliers

转载:http://www.cnblogs.com/jun-ma/p/4850591.html GuavaSuppliers的主要功能是创建包裹的单例对象,通过get方法可以获取对象的值。每次获取的对象都为同一个对象,但你和单例模式有所区别,Suppliers具备更加迷人的色彩。 Lazy初始... 查看详情

guava

Guava工具类学习一、概述  Guava是对JavaAPI的补充,对Java开发中常用功能进行更优雅的实现,使得编码更加轻松,代码容易理解。Guava使用了多种设计模式,同时经过了很多测试,得到了越来越多开发团队的青睐。Java最新版... 查看详情