当前位置: 首页 > news >正文

netty(三):NIO——多线程优化

NIO多线程优化

  • 使用Boss线程来处理accepct事件
  • 使用Worker线程来处理读写事件,可以创建多个worker线程
package com.review;import lombok.extern.slf4j.Slf4j;import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.*;
import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;@Slf4j
public class MultiServer {public static void main(String[] args) throws IOException {Thread.currentThread().setName("BOSS");Selector selector = Selector.open();ServerSocketChannel ssc = ServerSocketChannel.open();ssc.configureBlocking(false);ssc.bind(new InetSocketAddress(11027));SelectionKey sscKey = ssc.register(selector, 0, null);sscKey.interestOps(SelectionKey.OP_ACCEPT);Worker worker = new Worker("worker-0");while (true){selector.select();Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while (iterator.hasNext()){SelectionKey key = iterator.next();iterator.remove();if (key.isAcceptable()){ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();SocketChannel sc = serverSocketChannel.accept();sc.configureBlocking(false);worker.register(sc);worker.selector.wakeup();}}}}static class Worker implements Runnable{private String name;private Selector selector;private Thread thread;private volatile boolean start;ConcurrentLinkedQueue<Runnable> queue = new ConcurrentLinkedQueue<>();public Worker(String name){this.name = name;}public void register(SocketChannel sc) throws IOException {if (!start){selector = Selector.open();thread = new Thread(this,this.name);thread.start();start = true;}queue.add(()->{try {SelectionKey scKey = sc.register(selector, 0, null);scKey.interestOps(SelectionKey.OP_READ);} catch (ClosedChannelException e) {log.error(e.getMessage(),e);}});}@Overridepublic void run() {while (true){try {selector.select();} catch (IOException e) {log.error(e.getMessage(),e);}if (queue.size() > 0){queue.poll().run();}Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();while (iterator.hasNext()){SelectionKey selectionKey = iterator.next();iterator.remove();if (selectionKey.isReadable()){SocketChannel channel = (SocketChannel) selectionKey.channel();}}}}}
}

————本专栏来自于对满叔在b站所讲的netty课程的理解

http://www.lryc.cn/news/146414.html

相关文章:

  • Linux操作系统--linux概述
  • 数组中出现次数超过一半的数字
  • 网络优化工程师,你真的了解吗?
  • git 的常用命令
  • linux如何拷贝文件,删除多余的一级目录,用*号代替所有文件
  • springboot使用properties
  • Android中获取手机SIM卡的各种信息
  • matlab 根据索引提取点云
  • 蓝芯、四川邦辰面试(部分)
  • openCV实战-系列教程13:文档扫描OCR识别下(图像轮廓/模版匹配)项目实战、源码解读
  • SpringBootWeb案例 Part 4
  • 什么是ChatGPT水印,ChatGPT生成的内容如何不被检测出来,原理什么?
  • Android 6.0 Settings中添加虚拟键开关
  • Yolov8小目标检测(12):动态稀疏注意力BiFormer | CVPR 2023
  • C# VS调试技巧
  • VS的调试技巧
  • lucene国内镜像 极速下载
  • Qt 信号槽连接方式
  • (线程池) 100行以内的简单线程池
  • Mysql按姓氏从小到大排序的正确sql
  • 【C++】详细介绍模版初阶—函数模版、类模板
  • BananaPi BPI-6202工业控制板全志科技A40i、24V DC输入、RS485接口
  • Python - functools.partial设置回调函数处理异步任务基本使用
  • phpspreadsheet导出excel自动获得列,数字下标
  • 结算日-洛谷
  • Android Native Code开发学习(一)环境配置
  • Python GUI应用程序开发之wxPython使用详解
  • 【电子学会真题】青少年软件编程(C语言)等级考试试卷(一级) 2021年9月
  • 学习完毕JavaSE的感想
  • FastJson的学习