NioEventLoop類


NioEventLoop繼承關(guān)系圖

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)是將RunnableCallable封裝成ScheduledFutureTask,然后加到字段PriorityQueue<ScheduledFutureTask<?>>隊(duì)列中。
??隊(duì)列中的ScheduledFutureTask是按照剩余時間大小排列的,AbstractEventExecutor還提供了pollScheduledTask()獲取下一個到時的任務(wù)。

OrderedEventExecutor

??OrderedEventExecutor只是一個標(biāo)記接口,沒有任何方法,表示任務(wù)會按照順序執(zhí)行。

EventLoop

??EventLoop只是將父類parent()方法返回,從EventExecutorGroup,改為EventLoopGroup

public interface EventLoop extends OrderedEventExecutor, EventLoopGroup {
    @Override
    EventLoopGroup parent();
}

SingleThreadEventExecutor

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容