本文主要是介绍Guava之ListenableFuture(链式回调),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上一篇文章,只是简单地介绍了用guava的ListenableFuture实现异步,那如果需要多重回调呢?
方法 | 描述 |
---|---|
transform | 加一个回调函数 |
allAsList | 返回一个ListenableFuture ,该ListenableFuture 返回的result是一个List,List中的值是每个ListenableFuture的返回值,假如传入的其中之一fails或者cancel,这个Future fails 或者canceled |
successAsList | 返回一个ListenableFuture ,该Future的结果包含所有成功的Future,按照原来的顺序,当其中之一Failed或者cancel,则用null替代 |
链式回调:
package cn.dubby.listeningfuture;import com.google.common.util.concurrent.*;import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;/*** Created by dubby on 16/4/29.*/
public class ListeningFutureDemo {public static void main(String[] args) {final CountDownLatch latch = new CountDownLatch(1);final ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));ListenableFuture<String> explosion = service.submit(new Callable<String>() {public String call() throws Exception {System.out.println("任务线程正在执行...");try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}return "任务线程的结果";}});ListenableFuture<String> first = Futures.transform(explosion, new AsyncFunction<String, String>() {public ListenableFuture<String> apply(final String input) throws Exception {ListenableFuture<String> temp = service.submit(new Callable<String>() {public String call() throws Exception {System.out.println("第1个回调线程正在执行...");try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}return input + " & 第1个回调线程的结果 ";}});return temp;}}, service);ListenableFuture<String> second = Futures.transform(first, new AsyncFunction<String, String>() {public ListenableFuture<String> apply(final String input) throws Exception {ListenableFuture<String> temp = service.submit(new Callable<String>() {public String call() throws Exception {System.out.println("第2个回调线程正在执行...");try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}return input + " & 第2个回调线程的结果 ";}});return temp;}}, service);ListenableFuture<String> third = Futures.transform(second, new AsyncFunction<String, String>() {public ListenableFuture<String> apply(final String input) throws Exception {ListenableFuture<String> temp = service.submit(new Callable<String>() {public String call() throws Exception {System.out.println("第3个回调线程正在执行...");try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}return input + " & 第3个回调线程的结果 ";}});return temp;}}, service);ListenableFuture<String> forth = Futures.transform(third, new AsyncFunction<String, String>() {public ListenableFuture<String> apply(final String input) throws Exception {ListenableFuture<String> temp = service.submit(new Callable<String>() {public String call() throws Exception {System.out.println("第4个回调线程正在执行...");try {Thread.sleep(1000);} catch (Exception e) {e.printStackTrace();}return input + " & 第4个回调线程的结果 ";}});return temp;}}, service);Futures.addCallback(forth, new FutureCallback<String>() {public void onSuccess(String result) {latch.countDown();System.out.println("结果: " + result);}public void onFailure(Throwable t) {System.out.println(t.getMessage());}});try {latch.await();} catch (InterruptedException e) {e.printStackTrace();}service.shutdown();}}
一个Future,一共进行了两次处理,同样的,可以添加更多的回调函数。简化了操作。
这篇关于Guava之ListenableFuture(链式回调)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!