手把手教你调试Netty启动服务流程源码

192 阅读1分钟

我们自己的线程,如果写在main 方法,那就是

main 线程

  • 在创建 NioEventLoopGroup(创建一组NioEventLoop) 的过程中就创建了 selector
  • 这里因为我们当前线程其实是 main 线程,所以为 false
  • 创建 ServerSocketChannel
  • 初始化 server socket channel
  • 给 server socket channel 从 boss group 选择一个 NioEventLoop

boss thread

  • 将 serverSocketChannel 注册到选择的 NioEventLoop 的 selector

  • 绑定地址启动

  • 注册接受连接事件(OP_ACCEPT) 到selector

  • 第一次注册并非监听OP_ACCEPT,而是0

  • 最终监听的OP_ ACCEPT通过bind完成后的fireChannelActive()触发 NioEventLoop 是通过 Register 操作的执行来完成启动。