美文网首页Netty
NioEventLoop类

NioEventLoop类

作者: 骁兵 | 来源:发表于2019-03-11 16:21 被阅读0次

    NioEventLoop继承关系图

    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中执行。

    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没有实现具体的逻辑,主要是将实现代理给父类,或者抛出UnsupportedOperationException异常。

    AbstractScheduledEventExecutor

      AbstractScheduledEventExecutor实现了schedule()方法,具体实现是将RunnableCallable封装成ScheduledFutureTask,然后加到字段PriorityQueue<ScheduledFutureTask<?>>队列中。
      队列中的ScheduledFutureTask是按照剩余时间大小排列的,AbstractEventExecutor还提供了pollScheduledTask()获取下一个到时的任务。

    OrderedEventExecutor

      OrderedEventExecutor只是一个标记接口,没有任何方法,表示任务会按照顺序执行。

    EventLoop

      EventLoop只是将父类parent()方法返回,从EventExecutorGroup,改为EventLoopGroup

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

    SingleThreadEventExecutor

    相关文章

      网友评论

        本文标题:NioEventLoop类

        本文链接:https://www.haomeiwen.com/subject/yltbpqtx.html