Future
??netty包的Future
繼承了JDK里的Future
,主要是增加了addListener()
和removeListener()
方法。
public interface Future<V> extends java.util.concurrent.Future<V> {
boolean isSuccess();
boolean isCancellable();
Throwable cause();
Future<V> addListener(GenericFutureListener<? extends Future<? super V>> listener);
Future<V> addListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
Future<V> removeListener(GenericFutureListener<? extends Future<? super V>> listener);
Future<V> removeListeners(GenericFutureListener<? extends Future<? super V>>... listeners);
Future<V> sync() throws InterruptedException;
Future<V> syncUninterruptibly();
Future<V> await() throws InterruptedException;
Future<V> awaitUninterruptibly();
boolean await(long timeout, TimeUnit unit) throws InterruptedException;
boolean await(long timeoutMillis) throws InterruptedException;
boolean awaitUninterruptibly(long timeout, TimeUnit unit);
boolean awaitUninterruptibly(long timeoutMillis);
V getNow();
@Override
boolean cancel(boolean mayInterruptIfRunning);
}
Promise
??Promise
是一個可寫的Future
。
public interface Promise<V> extends Future<V> {
Promise<V> setSuccess(V result);
boolean trySuccess(V result);
Promise<V> setFailure(Throwable cause);
boolean tryFailure(Throwable cause);
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();
}
EventExecutor
??EventExecutor
主要是提供了inEventLoop()
方法查看一個線程是否在event loop中執(zhí)行沿癞。
public interface EventExecutor extends EventExecutorGroup {
//返回this
@Override
EventExecutor next();
EventExecutorGroup parent();
boolean inEventLoop();
boolean inEventLoop(Thread thread);
<V> Promise<V> newPromise();
<V> ProgressivePromise<V> newProgressivePromise();
//這里的Future是netty包里面的燎斩,而不是jdk里面的那個
<V> Future<V> newSucceededFuture(V result);
<V> Future<V> newFailedFuture(Throwable cause);
}
AbstractEventExecutor
??AbstractEventExecutor
沒有實(shí)現(xiàn)具體的邏輯,主要是將實(shí)現(xiàn)代理給父類霹抛,或者拋出UnsupportedOperationException
異常。
AbstractScheduledEventExecutor
??AbstractScheduledEventExecutor
實(shí)現(xiàn)了schedule()
方法卷谈,具體實(shí)現(xiàn)是將Runnable
或Callable
封裝成ScheduledFutureTask
杯拐,然后加到字段PriorityQueue<ScheduledFutureTask<?>>
隊列中。
??隊列中的ScheduledFutureTask是按照剩余時間大小排列的世蔗,AbstractEventExecutor
還提供了pollScheduledTask()
獲取下一個到時的任務(wù)端逼。
OrderedEventExecutor
??OrderedEventExecutor
只是一個標(biāo)記接口,沒有任何方法污淋,表示任務(wù)會按照順序執(zhí)行顶滩。
EventLoop
??EventLoop
只是將父類parent()
方法返回,從EventExecutorGroup
寸爆,改為EventLoopGroup
礁鲁。
public interface EventLoop extends OrderedEventExecutor, EventLoopGroup {
@Override
EventLoopGroup parent();
}