//1. netty中使用NioEventLoopGroup(简称nio boss线程)来封装线程和selector
Select selector = Selector.open();
//2. 创建NioServerSocketChannel 同时会初始化它关联的handler以及为原生ssc存储config
NioServerSocketChannel attachment = new NioServerSocketChannel();
//3. 创建NioServerSocketChannel时,创建了Java原生的ServerSocketChannel
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.configureBlocking(false);
//4 启动nio boss 线程执行接下来的操作
//5 注册(仅关联selector和NioServerSocketChannel)来关注事件
SelectionKey selectionKey = serverSocketChannel.register(selector,0,attachment);
//6 head -> 初始化器 -> ServerBootstrapAcceptor -> tail 初始化器一一次性的,只为添加acceptor
//7 绑定端口
serverSocketChannel.bind(new InetSocketAddress(8080));
//8 触发channel active事件 在head中关注op_accept事件
selectionKey.interestOps(SelectionKey.OP_ACCEPT);
Nio启动剖析
最新推荐文章于 2024-09-14 17:21:40 发布