天天看點

google Guava包的ListenableFuture解析 接口 添加回調(Callbacks) ListenableFuture的建立 Application CheckedFuture

我們強烈地建議你在代碼中多使用listenablefuture來代替jdk的 future, 因為:

大多數futures 方法中需要它。

轉到listenablefuture 程式設計比較容易。

guava提供的通用公共類封裝了公共的操作方方法,不需要提供future和listenablefuture的擴充方法。

傳統jdk中的future通過異步的方式計算傳回結果:在多線程運算中可能或者可能在沒有結束傳回結果,future是運作中的多線程的一個引用句柄,確定在服務執行傳回一個result。

listenablefuture可以允許你注冊回調方法(callbacks),在運算(多線程執行)完成的時候進行調用,  或者在運算(多線程執行)完成後立即執行。這樣簡單的改進,使得可以明顯的支援更多的操作,這樣的功能在jdk concurrent中的future是不支援的。

<code>01</code>

<code>listeningexecutorservice service = moreexecutors.listeningdecorator(executors.newfixedthreadpool(</code><code>10</code><code>));</code>

<code>02</code>

<code>listenablefuture explosion = service.submit(</code><code>new</code> <code>callable() {</code>

<code>03</code>

<code>  </code><code>public</code> <code>explosion call() {</code>

<code>04</code>

<code>    </code><code>return</code> <code>pushbigredbutton();</code>

<code>05</code>

<code>  </code><code>}</code>

<code>06</code>

<code>});</code>

<code>07</code>

<code>futures.addcallback(explosion, </code><code>new</code> <code>futurecallback() {</code>

<code>08</code>

<code>  </code><code>// we want this handler to run immediately after we push the big red button!</code>

<code>09</code>

<code>  </code><code>public</code> <code>void</code> <code>onsuccess(explosion explosion) {</code>

<code>10</code>

<code>    </code><code>walkawayfrom(explosion);</code>

<code>11</code>

<code>12</code>

<code>  </code><code>public</code> <code>void</code> <code>onfailure(throwable thrown) {</code>

<code>13</code>

<code>    </code><code>battlearchnemesis(); </code><code>// escaped the explosion!</code>

<code>14</code>

<code>15</code>

使用listenablefuture 最重要的理由是它可以進行一系列的複雜鍊式的異步操作。

<code>1</code>

<code>listenablefuture rowkeyfuture = indexservice.lookup(query);</code>

<code>2</code>

<code>asyncfunction&lt;rowkey, queryresult&gt; queryfunction =</code>

<code>3</code>

<code>new</code> <code>asyncfunction&lt;rowkey, queryresult&gt;() {</code>

<code>4</code>

<code>public</code> <code>listenablefuture apply(rowkey rowkey) {</code>

<code>5</code>

<code>return</code> <code>dataservice.read(rowkey);</code>

<code>6</code>

<code>}</code>

<code>7</code>

<code>};</code>

<code>8</code>

<code>listenablefuture queryfuture = futures.transform(rowkeyfuture, queryfunction, queryexecutor);</code>

其他更多的操作可以更加有效的支援而jdk中的future是沒法支援的.

不同的操作可以在不同的executors中執行,單獨的listenablefuture 可以有多個操作等待。

方法

描述

參考

傳回一個新的listenablefuture ,該listenablefuture 傳回的result是由傳入的asyncfunction 參數指派到傳入的 listenablefuture中.

<a href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/futures.html#transform(com.google.common.util.concurrent.listenablefuture,%20com.google.common.util.concurrent.asyncfunction)">transform(listenablefuture&lt;a&gt;, asyncfunction&lt;a, b&gt;)</a>

<a href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/futures.html#transform(com.google.common.util.concurrent.listenablefuture,%20com.google.common.base.function,%20java.util.concurrent.executor)">transform(listenablefuture&lt;a&gt;, function&lt;a, b&gt;, executor)</a>

傳回一個新的listenablefuture ,該listenablefuture 傳回的result是由傳入的function 參數指派到傳入的 listenablefuture中.

<a href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/futures.html#transform(com.google.common.util.concurrent.listenablefuture,%20com.google.common.base.function)">transform(listenablefuture&lt;a&gt;, function&lt;a, b&gt;)</a>

<a href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/futures.html#allaslist(java.lang.iterable)">allaslist(iterable&lt;listenablefuture&lt;v&gt;&gt;)</a>

傳回一個listenablefuture ,該listenablefuture 傳回的result是一個list,list中的值是每個listenablefuture的傳回值,假如傳入的其中之一fails或者cancel,這個future fails 或者canceled

<a href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/futures.html#allaslist(com.google.common.util.concurrent.listenablefuture...)">allaslist(listenablefuture&lt;v&gt;...)</a>

<a href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/futures.html#successfulaslist(java.lang.iterable)">successfulaslist(iterable&lt;listenablefuture&lt;v&gt;&gt;)</a>

傳回一個listenablefuture ,該future的結果包含所有成功的future,按照原來的順序,當其中之一failed或者cancel,則用null替代

<a href="http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/util/concurrent/futures.html#successfulaslist(com.google.common.util.concurrent.listenablefuture...)">successfulaslist(listenablefuture&lt;v&gt;...)</a>

<code>list&lt;listenablefuture&gt; queries;</code>

<code>// the queries go to all different data centers, but we want to wait until they're all done or failed.</code>

<code>listenablefuture&lt;list&gt; successfulqueries = futures.successfulaslist(queries);</code>

<code>futures.addcallback(successfulqueries, callbackonsuccessfulqueries);</code>