一稿械、前言
在分析Netty的源碼的時候,一直看到Future逼蒙、Promise之類的類,F(xiàn)uture之前還接觸過一點(diǎn)
知道大致是什么東西访忿,不過Promise沒了解過,所以在分析源碼之前,順便學(xué)習(xí)下Future/Promise~
二、Future
1囱挑、Future是什么?
首先需要明白一點(diǎn)的是,它不是netty自己私有的東西,它在jdk1.5后的concurrent包中就已經(jīng)有
了.
通過一個例子了解本質(zhì):
1醉顽、client向server提交任務(wù)
2、server端開啟一個線程去處理,但是也馬上返回一個Future給client
3平挑、client可能此時做一些其它操作
4游添、client獲取Future對象,調(diào)用Future的方法(比方說get())來獲取真正數(shù)據(jù)
代碼例子參考:http://www.cnblogs.com/wade-luffy/p/6229410.html
由圖可以看出,原生jdk提供的方法比較少.我們通過get()方法來獲取異步執(zhí)行的完成狀態(tài),如果執(zhí)行完成返回結(jié)果,否則一直等待.但是這個方法有一個弊端,就是如果任務(wù)一直沒完成,那就會處于一直等待的狀態(tài),即使有V get(long timeout, TimeUnit unit)這個函數(shù),
如果超過了超時時間也會報超長,netty提供了更好的方法.
下面看下netty中的:
我們需要知道的是在netty每個操作都是異步的,(應(yīng)該也不能這么說吧,應(yīng)該說成非阻塞,因?yàn)榈讓邮腔趎io,aio才是完全異步的).
它繼承了原生的Future,擴(kuò)充了方法,比方說:addListener(GenericFutureListener<? extends Future<? super V>>),Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);等方法.
添加監(jiān)聽器就解決了原生中g(shù)et方法存在的問題,當(dāng)任務(wù)完成之后,會主動調(diào)用operationComplete()方法.由此也可以提現(xiàn)netty的事件驅(qū)動
2系草、ChannelFuture
ChannelFuture是netty中異步Future,作用是用來保存Channel異步操作的結(jié)果,這個實(shí)例將給你一些關(guān)于I/O操作結(jié)果或者狀態(tài)的信息。
注意:調(diào)用await系列方法指定的等待超時時間和I/O超時時間之間是沒有特定關(guān)系的唆涝。如果沒有設(shè)置I/O超時時間找都,那么可能在await方法超時之后,future方法其實(shí)是沒有執(zhí)行完的廊酣。
2能耻、Promise
/**
* Special {@link Future} which is writable.
*/
public interface Promise<V> extends Future<V> {
/**
* Marks this future as a success and notifies all
* listeners.
*
* If it is success or failed already it will throw an {@link IllegalStateException}.
*/
Promise<V> setSuccess(V result);
/**
* Marks this future as a success and notifies all
* listeners.
*
* @return {@code true} if and only if successfully marked this future as
* a success. Otherwise {@code false} because this future is
* already marked as either a success or a failure.
*/
boolean trySuccess(V result);
/**
* Marks this future as a failure and notifies all
* listeners.
*
* If it is success or failed already it will throw an {@link IllegalStateException}.
*/
Promise<V> setFailure(Throwable cause);
/**
* Marks this future as a failure and notifies all
* listeners.
*
* @return {@code true} if and only if successfully marked this future as
* a failure. Otherwise {@code false} because this future is
* already marked as either a success or a failure.
*/
boolean tryFailure(Throwable cause);
/**
* Make this future impossible to cancel.
*
* @return {@code true} if and only if successfully marked this future as uncancellable or it is already done
* without being cancelled. {@code false} if this future has been cancelled already.
*/
boolean setUncancellable();
@Override
Promise<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);
@Override
Promise<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
@Override
Promise<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);
@Override
Promise<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
@Override
Promise<V> await() throws InterruptedException;
@Override
Promise<V> awaitUninterruptibly();
@Override
Promise<V> sync() throws InterruptedException;
@Override
Promise<V> syncUninterruptibly();
}
從上面可以看出其實(shí)Promise就是一個可寫的Future,沒有我想象的那么復(fù)雜.其實(shí)如果是理解了Future,那么Promise其實(shí)也是能夠理解了.從它繼承Future
和添加的幾個借口方法也能看出來它的大致用途.
三、總結(jié)
ok ,知道了什么是Future和Promise,接下來就可以開始netty源碼之旅了~~~