NIO this.selector.select()

过程梳理:

1. SelectorImpl.lockAndDoSelect()
2. windowsSelectorImpl.doSelect 正式开始轮询事件
	 2.1 subSelector.poll(); 开始底层的轮询,获取就绪文件描述符
	 2.2 this.updateSelectedKeys(); 将就绪的key加入到selectedKeys中,进入该方法
		2.2.1 this.subSelector.processSelectedKeys(this.updateCount);在主线程调用poll之后,会获取到已就绪的文件描述符(包含可读、可写、异常)。通过调用processSelectedKeys将就绪的文件描述符对应的SelectorKey加入到selectedKeys中。这样我们外部就可以调用到所有就绪的SelectorKey进行遍历处理。

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

SelectorImpl.lockAndDoSelect()

在这里插入图片描述

重点:最后执行SelectorImpl.doSelect,在windows平台由WindowsSelectorImpl实现

WindowsSelectorImpl#doSelect

在这里插入图片描述
在这里插入图片描述
一个个来分析

 protected int doSelect(long var1) throws IOException {
   
 //首先查看Selector中是否有Channel,没有就抛出异常
        if (this.channelArray == null) {
   
            throw new ClosedSelectorException();
        } else {
   
            this.timeout = var1;
            this.processDeregisterQueue();
             设置中断器,实际调用的是AbstractSelector.this.wakeup();方法
    // 调用的是方法AbstractInterruptibleChannel.blockedOn(Interruptible);
            if (this.interruptTriggered) {
   
                this.resetWakeupSocket();
                return 0;
            } else {
   
            //调整辅助线程数量
                this.adjustThreadsCount();
                //调整主线程与辅助线程之间的运行关系
                this.finishLock.reset();
                this.startLock.startThreads();

                try {
   
                    this.begin();

                    try {
   
                    //执行poll方法
                        this.subSelector.poll
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值