ListenableFuture

来源:互联网 发布:java包装一年工作经验 编辑:程序博客网 时间:2024/05/21 17:59

ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强。我们知道Future表示一个异步计算任务,当任务完成时可以得到计算结果。如果我们希望一旦计算完成就拿到结果展示给用户或者做另外的计算,就必须使用另一个线程不断的查询计算状态。这样做,代码复杂,而且效率低下。使用ListenableFuture Guava帮我们检测Future是否完成了,如果完成就自动调用回调函数,这样可以减少并发程序的复杂度。

ListenableFuture是一个接口,它从jdk的Future接口继承,添加了void addListener(Runnable listener, Executor executor)方法。

我们看下如何使用ListenableFuture。首先需要定义ListenableFuture的实例。

        ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());        final ListenableFuture<Integer> listenableFuture = executorService.submit(new Callable<Integer>() {            @Override            public Integer call() throws Exception {                System.out.println("call execute..");                TimeUnit.SECONDS.sleep(1);                return 7;            }        });

首先通过MoreExecutors类的静态方法listeningDecorator方法初始化一个ListeningExecutorService的方法,然后使用此实例的submit方法即可初始化ListenableFuture对象。

我们上文中定义的ListenableFuture要做的工作,在Callable接口的实现类中定义,这里只是休眠了1秒钟然后返回一个数字7.

有了ListenableFuture实例,有两种方法可以执行此Future并执行Future完成之后的回调函数。

方法一:通过ListenableFuture的addListener方法


        listenableFuture.addListener(new Runnable() {            @Override            public void run() {                try {                    System.out.println("get listenable future's result " + listenableFuture.get());                } catch (InterruptedException e) {                    e.printStackTrace();                } catch (ExecutionException e) {                    e.printStackTrace();                }            }        }, executorService);

方法二:通过Futures的静态方法addCallback给ListenableFuture添加回调函数

        Futures.addCallback(listenableFuture, new FutureCallback<Integer>() {            @Override            public void onSuccess(Integer result) {                System.out.println("get listenable future's result with callback " + result);            }            @Override            public void onFailure(Throwable t) {                t.printStackTrace();            }        });

推荐使用第二种方法,因为第二种方法可以直接得到Future的返回值,或者处理错误情况。本质上第二种方法是通过调动第一种方法实现的,做了进一步的封装。

另外ListenableFuture还有其他几种内置实现:

  1. SettableFuture:不需要实现一个方法来计算返回值,而只需要返回一个固定值来做为返回值,可以通过程序设置此Future的返回值或者异常信息
  2. CheckedFuture: 这是一个继承自ListenableFuture接口,他提供了checkedGet()方法,此方法在Future执行发生异常时,可以抛出指定类型的异常。

google Guava包的ListenableFuture解析

原文地址  译者:罗立树  校对:方腾飞

并发编程是一个难题,但是一个强大而简单的抽象可以显著的简化并发的编写。出于这样的考虑,Guava 定义了 ListenableFuture接口并继承了JDK concurrent包下的Future 接口。

我们强烈地建议你在代码中多使用ListenableFuture来代替JDK的 Future, 因为:

  • 大多数Futures 方法中需要它。
  • 转到ListenableFuture 编程比较容易。
  • Guava提供的通用公共类封装了公共的操作方方法,不需要提供Future和ListenableFuture的扩展方法。

接口

传统JDK中的Future通过异步的方式计算返回结果:在多线程运算中可能或者可能在没有结束返回结果,Future是运行中的多线程的一个引用句柄,确保在服务执行返回一个Result。

ListenableFuture可以允许你注册回调方法(callbacks),在运算(多线程执行)完成的时候进行调用,  或者在运算(多线程执行)完成后立即执行。这样简单的改进,使得可以明显的支持更多的操作,这样的功能在JDK concurrent中的Future是不支持的。

ListenableFuture 中的基础方法是addListener(Runnable, Executor), 该方法会在多线程运算完的时候,指定的Runnable参数传入的对象会被指定的Executor执行。

添加回调(Callbacks)

多数用户喜欢使用 Futures.addCallback(ListenableFuture<V>, FutureCallback<V>, Executor)的方式, 或者 另外一个版本version(译者注:addCallback(ListenableFuture<V> future,FutureCallback<? super V> callback)),默认是采用 MoreExecutors.sameThreadExecutor()线程池, 为了简化使用,Callback采用轻量级的设计.  FutureCallback<V> 中实现了两个方法:

  • onSuccess(V),在Future成功的时候执行,根据Future结果来判断。
  • onFailure(Throwable), 在Future失败的时候执行,根据Future结果来判断。

ListenableFuture的创建

对应JDK中的 ExecutorService.submit(Callable) 提交多线程异步运算的方式,Guava 提供了ListeningExecutorService 接口, 该接口返回 ListenableFuture 而相应的 ExecutorService 返回普通的 Future。将 ExecutorService 转为 ListeningExecutorService,可以使用MoreExecutors.listeningDecorator(ExecutorService)进行装饰。

01ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10));
02ListenableFuture explosion = service.submit(newCallable() {
03  publicExplosion call() {
04    returnpushBigRedButton();
05  }
06});
07Futures.addCallback(explosion, newFutureCallback() {
08  // we want this handler to run immediately after we push the big red button!
09  publicvoid onSuccess(Explosion explosion) {
10    walkAwayFrom(explosion);
11  }
12  publicvoid onFailure(Throwable thrown) {
13    battleArchNemesis();// escaped the explosion!
14  }
15});

另外, 假如你是从 FutureTask转换而来的, Guava 提供ListenableFutureTask.create(Callable<V>) 和ListenableFutureTask.create(Runnable, V). 和 JDK不同的是, ListenableFutureTask 不能随意被继承(译者注:ListenableFutureTask中的done方法实现了调用listener的操作)。

假如你喜欢抽象的方式来设置future的值,而不是想实现接口中的方法,可以考虑继承抽象类AbstractFuture<V> 或者直接使用 SettableFuture 。

假如你必须将其他API提供的Future转换成 ListenableFuture,你没有别的方法只能采用硬编码的方式JdkFutureAdapters.listenInPoolThread(Future) 来将 Future 转换成 ListenableFuture。尽可能地采用修改原生的代码返回 ListenableFuture会更好一些。

Application

使用ListenableFuture 最重要的理由是它可以进行一系列的复杂链式的异步操作。

1ListenableFuture rowKeyFuture = indexService.lookUp(query);
2AsyncFunction<RowKey, QueryResult> queryFunction =
3new AsyncFunction<RowKey, QueryResult>() {
4public ListenableFuture apply(RowKey rowKey) {
5return dataService.read(rowKey);
6}
7};
8ListenableFuture queryFuture = Futures.transform(rowKeyFuture, queryFunction, queryExecutor);

其他更多的操作可以更加有效的支持而JDK中的Future是没法支持的.

不同的操作可以在不同的Executors中执行,单独的ListenableFuture 可以有多个操作等待。

当一个操作开始的时候其他的一些操作也会尽快开始执行–“fan-out”–ListenableFuture 能够满足这样的场景:促发所有的回调(callbacks)。反之更简单的工作是,同样可以满足“fan-in”场景,促发ListenableFuture 获取(get)计算结果,同时其它的Futures也会尽快执行:可以参考 the implementation of Futures.allAsList 。(译者注:fan-in和fan-out是软件设计的一个术语,可以参考这里:http://baike.baidu.com/view/388892.htm#1或者看这里的解析Design Principles: Fan-In vs Fan-Out,这里fan-out的实现就是封装的ListenableFuture通过回调,调用其它代码片段。fan-in的意义是可以调用其它Future

方法描述参考transform(ListenableFuture<A>, AsyncFunction<A, B>, Executor)*返回一个新的ListenableFuture ,该ListenableFuture 返回的result是由传入的AsyncFunction 参数指派到传入的 ListenableFuture中.transform(ListenableFuture<A>, AsyncFunction<A, B>)transform(ListenableFuture<A>, Function<A, B>, Executor)返回一个新的ListenableFuture ,该ListenableFuture 返回的result是由传入的Function 参数指派到传入的 ListenableFuture中.transform(ListenableFuture<A>, Function<A, B>)allAsList(Iterable<ListenableFuture<V>>)返回一个ListenableFuture ,该ListenableFuture 返回的result是一个List,List中的值是每个ListenableFuture的返回值,假如传入的其中之一fails或者cancel,这个Future fails 或者canceledallAsList(ListenableFuture<V>...)successfulAsList(Iterable<ListenableFuture<V>>)返回一个ListenableFuture ,该Future的结果包含所有成功的Future,按照原来的顺序,当其中之一Failed或者cancel,则用null替代successfulAsList(ListenableFuture<V>...)

AsyncFunction<A, B> 中提供一个方法ListenableFuture<B> apply(A input),它可以被用于异步变换值。

1List<ListenableFuture> queries;
2// The queries go to all different data centers, but we want to wait until they're all done or failed.
3 
4ListenableFuture<List> successfulQueries = Futures.successfulAsList(queries);
5 
6Futures.addCallback(successfulQueries, callbackOnSuccessfulQueries);

CheckedFuture

Guava也提供了 CheckedFuture<V, X extends Exception> 接口。CheckedFuture 是一个ListenableFuture ,其中包含了多个版本的get 方法,方法声明抛出检查异常.这样使得创建一个在执行逻辑中可以抛出异常的Future更加容易 。将 ListenableFuture 转换成CheckedFuture,可以使用 Futures.makeChecked(ListenableFuture<V>, Function<Exception, X>)。


  1. package com.demo.learn;  
  2.   
  3. import java.util.List;  
  4. import java.util.concurrent.Callable;  
  5. import java.util.concurrent.ExecutionException;  
  6. import java.util.concurrent.ExecutorService;  
  7. import java.util.concurrent.Executors;  
  8. import java.util.concurrent.Future;  
  9.   
  10. import com.google.common.util.concurrent.FutureCallback;  
  11. import com.google.common.util.concurrent.Futures;  
  12. import com.google.common.util.concurrent.ListenableFuture;  
  13. import com.google.common.util.concurrent.ListeningExecutorService;  
  14. import com.google.common.util.concurrent.MoreExecutors;  
  15.   
  16. public class ListenableFutureDemo {  
  17.     static ListeningExecutorService service = MoreExecutors  
  18.             .listeningDecorator(Executors.newFixedThreadPool(10));  
  19.   
  20.     static ExecutorService eservice = Executors.newFixedThreadPool(10);  
  21.   
  22.     @SuppressWarnings("unchecked")  
  23.     public static void parallel() {  
  24.         final long start = System.currentTimeMillis();  
  25.         ListenableFuture<Object> taskFuture1 = service.submit(new Task1());  
  26.         ListenableFuture<Object> taskFuture2 = service.submit(new Task2());  
  27.         ListenableFuture<Object> taskFuture3 = service.submit(new Task3());  
  28.   
  29.         ListenableFuture<List<Object>> successfulQueries = Futures  
  30.                 .successfulAsList(taskFuture1, taskFuture2, taskFuture3);  
  31.         Futures.addCallback(successfulQueries, new FutureCallback<Object>() {  
  32.             public void onFailure(Throwable arg0) {  
  33.                 System.out.println(arg0);  
  34.             }  
  35.   
  36.             public void onSuccess(Object arg0) {  
  37.                 System.out.println(arg0);  
  38.                 System.out.println("parallel:"  
  39.                         + (System.currentTimeMillis() - start));  
  40.             }  
  41.         });  
  42.   
  43.     }  
  44.   
  45.     public static void synchronous() {  
  46.         final long start = System.currentTimeMillis();  
  47.         Future<Object> taskFuture1 = eservice.submit(new Task1());  
  48.         Future<Object> taskFuture2 = eservice.submit(new Task2());  
  49.         Future<Object> taskFuture3 = eservice.submit(new Task3());  
  50.   
  51.         try {  
  52.             Object result1 = taskFuture1.get();  
  53.             Object result2 = taskFuture2.get();  
  54.             Object result3 = taskFuture3.get();  
  55.             System.out.println(result1 + " " + result2 + " " + result3);  
  56.             System.out.println("synchronous:"  
  57.                     + (System.currentTimeMillis() - start));  
  58.         } catch (InterruptedException e) {  
  59.             e.printStackTrace();  
  60.         } catch (ExecutionException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.     }  
  64.   
  65.     public static void main(String[] args) {  
  66.         synchronous();  
  67.         parallel();  
  68.   
  69.         shutdown();  
  70.     }  
  71.   
  72.     private static void shutdown() {  
  73.         service.shutdown();  
  74.         eservice.shutdown();  
  75.     }  
  76.   
  77.     public static class Task1 implements Callable<Object> {  
  78.   
  79.         public Object call() throws Exception {  
  80.             Thread.sleep(5000);  
  81.             return "Task1";  
  82.         }  
  83.     }  
  84.   
  85.     public static class Task2 implements Callable<Object> {  
  86.   
  87.         public Object call() throws Exception {  
  88.             Thread.sleep(3000);  
  89.             return "Task2";  
  90.         }  
  91.     }  
  92.   
  93.     public static class Task3 implements Callable<Object> {  
  94.   
  95.         public Object call() throws Exception {  
  96.             Thread.sleep(4000);  
  97.             return 3;  
  98.         }  
  99.     }  
  100.   
  101. }  


原创粉丝点击