Netty NioEventLoop源码解读

  NioEventLoop中维护了一个线程,线程启动时会调用NioEventLoop的run方法,执行I/O任务和非I/O任务:I/O任务:即selectionKey中ready的事件,如accept、connect、read、write等,由processSelectedKeys方法触发。非IO任务:添加到taskQueue中的任务,如register0、bind0等任务,由runAllTasks方法触发。两种任务的执行时间比由变量ioRatio控制,默认为50,则表示允许非IO任务执行的时间与IO任务的执行时间相等。以下是NioEventLoop的构造方法。

//这是NioEventLoop的构造方法,NioEventLoopGroup 是管理NioEventLoop的集合,executor则为任务执行池,SelectorProvider 用于构造Selector对象,RejectedExecutionHandler用于在executor满了执行的逻辑
    NioEventLoop(NioEventLoopGroup parent, Executor executor, SelectorProvider selectorProvider,
                 SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler) {
        super(parent, executor, false, DEFAULT_MAX_PENDING_TASKS, rejectedExecutionHandler);
        if (selectorProvider == null) {
            throw new NullPointerException("selectorProvider");
        }
        if (strategy == null) {
            throw new NullPointerException("selectStrategy");
        }
        provider = selectorProvider;
        final SelectorTuple selectorTuple = openSelector();
        selector = selectorTuple.selector;
        unwrappedSelector = selectorTuple.unwrappedSelector;
        selectStrategy = strategy;
    }

在NioEventLoop类中,最重要的便是run方法,以下是run方法的源码:

    @Override
    protected void run() {
//这里是个死循环,里面的run方法逻辑会一直执行
        for (;;) {
            try {
//根据是队列里是否有任务执行不同的策略
                switch (selectStrategy.calculateStrategy(selectNowSupplier, hasTasks())) {
                    case SelectStrategy.CONTINUE:
                        continue;
                    case SelectStrategy.SELECT:
//select内部具体的方法看下面的源码
                        select(wakenUp.getAndSet(false));
                        if (wakenUp.get()) {
                            selector.wakeup();
                        }
                    default:
                }

                cancelledKeys = 0;
                needsToSelectAgain = false;
//这里是I/O与executor里任务执行的比率,如果设为100则表示I/O的传先级最高
                final int ioRatio = this.ioRatio;
                if (ioRatio == 100) {
                    try {
//处理网络I/O事件processSelectedKeys可以看下面的源码
                        processSelectedKeys();
                    } finally {
                      //最终不是会执行任务队列里的任务的
                        runAllTasks();
                    }
                } else {
                    final long ioStartTime = System.nanoTime();
                    try {
                        processSelectedKeys();
                    } finally {
                        //传入一个执行了io的时间,根据io执行时间算出任务能够执行多长时间
                        final long ioTime = System.nanoTime() - ioStartTime;
                      // runAllTasks看下面的源码
                        runAllTasks(ioTime * (100 - ioRatio) / ioRatio);
                    }
                }
            } catch (Throwable t) {
                handleLoopException(t);
            }
            // Always handle shutdown even if the loop processing threw an exception.
            try {
                if (isShuttingDown()) {
                    closeAll();
                    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值