💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 5.1-google Guava包的ListenableFuture解析 # 5.1-google Guava包的ListenableFuture解析 [原文地址](http://code.google.com/p/guava-libraries/wiki/ListenableFutureExplained) 译者:**罗立树** 校对:**方腾飞** 并发编程是一个难题,但是一个强大而简单的抽象可以显著的简化并发的编写。出于这样的考虑,Guava 定义了 [ListenableFuture](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/ListenableFuture.html)接口并继承了JDK concurrent包下的`Future` 接口。 **我们强烈地建议你在代码中多使用`ListenableFuture来代替JDK的``Future`**, 因为: - `大多数Futures` 方法中需要它。 - 转到`ListenableFuture` 编程比较容易。 - Guava提供的通用公共类封装了公共的操作方方法,不需要提供Future和`ListenableFuture的扩展方法。` ## 接口 传统JDK中的Future通过异步的方式计算返回结果:在多线程运算中可能或者可能在没有结束返回结果,Future是运行中的多线程的一个引用句柄,确保在服务执行返回一个Result。 ListenableFuture可以允许你注册回调方法(callbacks),在运算(多线程执行)完成的时候进行调用, 或者在运算(多线程执行)完成后立即执行。这样简单的改进,使得可以明显的支持更多的操作,这样的功能在JDK concurrent中的Future是不支持的。 `ListenableFuture` 中的基础方法是[`addListener(Runnable, Executor)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6ommon/util/concurrent/ListenableFuture.html#addListener%28java.lang.Runnable,%20java.util.concurrent.Executor%29), 该方法会在多线程运算完的时候,指定的Runnable参数传入的对象会被指定的Executor执行。 ## 添加回调(Callbacks) 多数用户喜欢使用 [Futures.addCallback(ListenableFuture<V>, FutureCallback<V>, Executor)](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#addCallback%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.util.concurrent.FutureCallback,%20java.util.concurrent.Executor%29)的方式, 或者 另外一个版本[version](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#addCallback%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.util.concurrent.FutureCallback%29)(译者注:[addCallback](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/src-html/com/google/common/util/concurrent/Futures.html#line.1106)([ListenableFuture](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/ListenableFuture.html "interface in com.google.common.util.concurrent")<V> future,[FutureCallback](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/FutureCallback.html "interface in com.google.common.util.concurrent")<? super V> callback)),默认是采用 `MoreExecutors.sameThreadExecutor()线程池`, 为了简化使用,Callback采用轻量级的设计. [`FutureCallback&lt;V&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/FutureCallback.html) 中实现了两个方法: - [`onSuccess(V)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/FutureCallback.html#onSuccess%28V%29),在Future成功的时候执行,根据Future结果来判断。 - [`onFailure(Throwable)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6e/common/util/concurrent/FutureCallback.html#onFailure%28java.lang.Throwable%29), 在Future失败的时候执行,根据Future结果来判断。 ## ListenableFuture的创建 对应JDK中的 [`ExecutorService.submit(Callable)`](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ExecutorService.html#submit%28java.util.concurrent.Callable%29) 提交多线程异步运算的方式,Guava 提供了[`ListeningExecutorService`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/ListeningExecutorService.html) 接口, 该接口返回 `ListenableFuture` 而相应的 `ExecutorService` 返回普通的 `Future`。将 `ExecutorService` 转为 `ListeningExecutorService,`可以使用[MoreExecutors.listeningDecorator(ExecutorService)](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6MoreExecutors.html#listeningDecorator%28java.util.concurrent.ExecutorService%29)进行装饰。 ``` <pre class="calibre11">``` ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); ListenableFuture explosion = service.submit(new Callable() { public Explosion call() { return pushBigRedButton(); } }); Futures.addCallback(explosion, new FutureCallback() { // we want this handler to run immediately after we push the big red button! public void onSuccess(Explosion explosion) { walkAwayFrom(explosion); } public void onFailure(Throwable thrown) { battleArchNemesis(); // escaped the explosion! } }); ``` ``` 另外, 假如你是从 [FutureTask](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/FutureTask.html)转换而来的, Guava 提供[`ListenableFutureTask.create(Callable&lt;V&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6/concurrent/ListenableFutureTask.html#create%28java.util.concurrent.Callable%29) 和[`ListenableFutureTask.create(Runnable, V)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6common/util/concurrent/ListenableFutureTask.html#create%28java.lang.Runnable,%20V%29). 和 JDK不同的是, `ListenableFutureTask` 不能随意被继承(译者注:ListenableFutureTask中的done方法实现了调用listener的操作)。 假如你喜欢抽象的方式来设置future的值,而不是想实现接口中的方法,可以考虑继承抽象类[`AbstractFuture&lt;V&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/AbstractFuture.html) 或者直接使用 [`SettableFuture`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/SettableFuture.html) 。 假如你必须将其他API提供的Future转换成 `ListenableFuture`,你没有别的方法只能采用硬编码的方式[`JdkFutureAdapters.listenInPoolThread(Future)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/JdkFutureAdapters.html) 来将 `Future` 转换成 `ListenableFuture`。尽可能地采用修改原生的代码返回 `ListenableFuture`会更好一些。 ## Application 使用`ListenableFuture` 最重要的理由是它可以进行一系列的复杂链式的异步操作。 ``` <pre class="calibre11">``` ListenableFuture rowKeyFuture = indexService.lookUp(query); AsyncFunction<RowKey, QueryResult> queryFunction = new AsyncFunction<RowKey, QueryResult>() { public ListenableFuture apply(RowKey rowKey) { return dataService.read(rowKey); } }; ListenableFuture queryFuture = Futures.transform(rowKeyFuture, queryFunction, queryExecutor); ``` ``` 其他更多的操作可以更加有效的支持而JDK中的Future是没法支持的. 不同的操作可以在不同的Executors中执行,单独的`ListenableFuture` 可以有多个操作等待。 当一个操作开始的时候其他的一些操作也会尽快开始执行–“fan-out”–`ListenableFuture` 能够满足这样的场景:促发所有的回调(callbacks)。反之更简单的工作是,同样可以满足“fan-in”场景,促发`ListenableFuture` 获取(get)计算结果,同时其它的Futures也会尽快执行:可以参考 [the implementation of `Futures.allAsList`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/src-html/com/google/common/util/concurrent/Futures.html#line.1276) 。(译者注:fan-in和fan-out是软件设计的一个术语,可以参考这里: <http://baike.baidu.com/view/388892.htm#1> 或者看这里的解析 [Design Principles: Fan-In vs Fan-Out](http://it.toolbox.com/blogs/enterprise-solutions/design-principles-fanin-vs-fanout-16088 "Design Principles: Fan-In vs Fan-Out") ,这里fan-out的实现就是封装的ListenableFuture通过回调,调用其它代码片段。fan-in的意义是可以调用其它Future) 方法描述参考[`transform(ListenableFuture&lt;A&gt;, AsyncFunction&lt;A, B&gt;, Executor)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6/Futures.html#transform%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.util.concurrent.AsyncFunction,%20java.util.concurrent.Executor%29)`*``返回一个新的ListenableFuture` ,该`ListenableFuture` 返回的result是由传入的`AsyncFunction` 参数指派到传入的 `ListenableFuture中`.[`transform(ListenableFuture&lt;A&gt;, AsyncFunction&lt;A, B&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6/Futures.html#transform%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.util.concurrent.AsyncFunction%29)[`transform(ListenableFuture&lt;A&gt;, Function&lt;A, B&gt;, Executor)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6/Futures.html#transform%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function,%20java.util.concurrent.Executor%29)`返回一个新的ListenableFuture` ,该`ListenableFuture` 返回的result是由传入的`Function` 参数指派到传入的 `ListenableFuture中`.[`transform(ListenableFuture&lt;A&gt;, Function&lt;A, B&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6/Futures.html#transform%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function%29)[`allAsList(Iterable&lt;ListenableFuture&lt;V&gt;&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/Futures.html#allAsList%28java.lang.Iterable%29)`返回一个ListenableFuture` ,该`ListenableFuture` 返回的result是一个List,List中的值是每个ListenableFuture的返回值,假如传入的其中之一fails或者cancel,这个Future fails 或者canceled[`allAsList(ListenableFuture&lt;V&gt;...)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6/Futures.html#allAsList%28com.google.common.util.concurrent.ListenableFuture...%29)[`successfulAsList(Iterable&lt;ListenableFuture&lt;V&gt;&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6le/common/util/concurrent/Futures.html#successfulAsList%28java.lang.Iterable%29)`返回一个ListenableFuture` ,该Future的结果包含所有成功的Future,按照原来的顺序,当其中之一Failed或者cancel,则用null替代[`successfulAsList(ListenableFuture&lt;V&gt;...)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6s.html#successfulAsList%28com.google.common.util.concurrent.ListenableFuture...%29)[`AsyncFunction&lt;A, B&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/AsyncFunction.html) 中提供一个方法`ListenableFuture&lt;B&gt; apply(A input),`它可以被用于异步变换值。 ``` <pre class="calibre11">``` List<ListenableFuture> queries; // The queries go to all different data centers, but we want to wait until they're all done or failed. ListenableFuture<List> successfulQueries = Futures.successfulAsList(queries); Futures.addCallback(successfulQueries, callbackOnSuccessfulQueries); ``` ``` ## CheckedFuture Guava也提供了 [`CheckedFuture&lt;V, X extends Exception&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/CheckedFuture.html) 接口。`CheckedFuture` 是一个`ListenableFuture` ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易 。将 `ListenableFuture` 转换成`CheckedFuture`,可以使用 [`Futures.makeChecked(ListenableFuture&lt;V&gt;, Function&lt;Exception, X&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#makeChecked%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function%29)。 Guava也提供了 [`CheckedFuture&lt;V, X extends Exception&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/CheckedFuture.html) 接口。`CheckedFuture` 是一个`ListenableFuture` ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易 。将 `ListenableFuture` 转换成`CheckedFuture`,可以使用 [`Futures.makeChecked(ListenableFuture&lt;V&gt;, Function&lt;Exception, X&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#makeChecked%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function%29)。 Guava也提供了 [`CheckedFuture&lt;V, X extends Exception&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/CheckedFuture.html) 接口。`CheckedFuture` 是一个`ListenableFuture` ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易 。将 `ListenableFuture` 转换成`CheckedFuture`,可以使用 [`Futures.makeChecked(ListenableFuture&lt;V&gt;, Function&lt;Exception, X&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#makeChecked%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function%29)。 Guava也提供了 [`CheckedFuture&lt;V, X extends Exception&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/CheckedFuture.html) 接口。`CheckedFuture` 是一个`ListenableFuture` ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易 。将 `ListenableFuture` 转换成`CheckedFuture`,可以使用 [`Futures.makeChecked(ListenableFuture&lt;V&gt;, Function&lt;Exception, X&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#makeChecked%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function%29)。 Guava也提供了 [`CheckedFuture&lt;V, X extends Exception&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/CheckedFuture.html) 接口。`CheckedFuture` 是一个`ListenableFuture` ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易 。将 `ListenableFuture` 转换成`CheckedFuture`,可以使用 [`Futures.makeChecked(ListenableFuture&lt;V&gt;, Function&lt;Exception, X&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#makeChecked%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function%29)。 Guava也提供了 [`CheckedFuture&lt;V, X extends Exception&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/CheckedFuture.html) 接口。`CheckedFuture` 是一个`ListenableFuture` ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易 。将 `ListenableFuture` 转换成`CheckedFuture`,可以使用 [`Futures.makeChecked(ListenableFuture&lt;V&gt;, Function&lt;Exception, X&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#makeChecked%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function%29)。 Guava也提供了 [`CheckedFuture&lt;V, X extends Exception&gt;`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/CheckedFuture.html) 接口。`CheckedFuture` 是一个`ListenableFuture` ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易 。将 `ListenableFuture` 转换成`CheckedFuture`,可以使用 [`Futures.makeChecked(ListenableFuture&lt;V&gt;, Function&lt;Exception, X&gt;)`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/%E2%80%A6utures.html#makeChecked%28com.google.common.util.concurrent.ListenableFuture,%20com.google.common.base.Function%29)。