欢迎光临
我们一直在努力

硬核图解:从BIO到AIO,五种I/O模型一网打尽!

硬核图解:从BIO到AIO,五种I/O模型一网打尽!

    • 1. 开篇:I/O模型是什么?为什么重要?
    • 2. 快速概览:五大I/O模型对比
    • 3. 模型一:阻塞I/O(BIO)
      • 3.1 核心流程
      • 3.2 特点
      • 3.3 Java代码示例
    • 4. 模型二:非阻塞I/O(NIO)
      • 4.1 核心流程
      • 4.2 非阻塞I/O流程图
      • 4.3 特点
    • 5. 模型三:I/O多路复用
      • 5.1 核心流程
      • 5.2 多路复用核心思想
      • 5.3 select/poll/epoll 演进
      • 5.4 多路复用工作流程
      • 5.5 Java NIO代码示例
    • 6. 模型四:信号驱动I/O
      • 6.1 核心流程
      • 6.2 特点
    • 7. 模型五:异步I/O(AIO)
      • 7.1 核心流程
      • 7.2 异步I/O vs 其他模型
      • 7.3 Java AIO示例
    • 8. 五大模型对比总结
      • 总结表
    • 9. 实战选型:我该用哪个?
      • 业界实践
    • 10. 进阶:io_uring——Linux的新一代异步I/O
    • 11. 面试高频追问
      • Q1:Java的NIO是NIO(非阻塞)还是多路复用?
      • Q2:为什么说Netty高性能?
      • Q3:select的1024限制是什么?
      • Q4:阻塞和非阻塞的区别是什么?
    • 12. 总结脑图
    • 13. 一句话总结

🌺The Begin🌺点点关注,收藏不迷路🌺

⬇ ⬇ 底部 ⬇ ⬇

1. 开篇:I/O模型是什么?为什么重要?

一次简单的数据读取,从应用程序发起请求到数据真正就绪,中间要经过操作系统内核、硬件中断、DMA等多层协作。不同的处理方式,就形成了不同的I/O模型。

I/O模型的核心区别在于两个阶段:

#mermaid-svg-9YEbX3Yhk9BS7cvF{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-9YEbX3Yhk9BS7cvF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9YEbX3Yhk9BS7cvF .error-icon{fill:#552222;}#mermaid-svg-9YEbX3Yhk9BS7cvF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9YEbX3Yhk9BS7cvF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9YEbX3Yhk9BS7cvF .marker.cross{stroke:#333333;}#mermaid-svg-9YEbX3Yhk9BS7cvF svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9YEbX3Yhk9BS7cvF p{margin:0;}#mermaid-svg-9YEbX3Yhk9BS7cvF .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-9YEbX3Yhk9BS7cvF .cluster-label text{fill:#333;}#mermaid-svg-9YEbX3Yhk9BS7cvF .cluster-label span{color:#333;}#mermaid-svg-9YEbX3Yhk9BS7cvF .cluster-label span p{background-color:transparent;}#mermaid-svg-9YEbX3Yhk9BS7cvF .label text,#mermaid-svg-9YEbX3Yhk9BS7cvF span{fill:#333;color:#333;}#mermaid-svg-9YEbX3Yhk9BS7cvF .node rect,#mermaid-svg-9YEbX3Yhk9BS7cvF .node circle,#mermaid-svg-9YEbX3Yhk9BS7cvF .node ellipse,#mermaid-svg-9YEbX3Yhk9BS7cvF .node polygon,#mermaid-svg-9YEbX3Yhk9BS7cvF .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-9YEbX3Yhk9BS7cvF .rough-node .label text,#mermaid-svg-9YEbX3Yhk9BS7cvF .node .label text,#mermaid-svg-9YEbX3Yhk9BS7cvF .image-shape .label,#mermaid-svg-9YEbX3Yhk9BS7cvF .icon-shape .label{text-anchor:middle;}#mermaid-svg-9YEbX3Yhk9BS7cvF .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-9YEbX3Yhk9BS7cvF .rough-node .label,#mermaid-svg-9YEbX3Yhk9BS7cvF .node .label,#mermaid-svg-9YEbX3Yhk9BS7cvF .image-shape .label,#mermaid-svg-9YEbX3Yhk9BS7cvF .icon-shape .label{text-align:center;}#mermaid-svg-9YEbX3Yhk9BS7cvF .node.clickable{cursor:pointer;}#mermaid-svg-9YEbX3Yhk9BS7cvF .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-9YEbX3Yhk9BS7cvF .arrowheadPath{fill:#333333;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-9YEbX3Yhk9BS7cvF .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9YEbX3Yhk9BS7cvF .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-9YEbX3Yhk9BS7cvF .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9YEbX3Yhk9BS7cvF .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-9YEbX3Yhk9BS7cvF .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-9YEbX3Yhk9BS7cvF .cluster text{fill:#333;}#mermaid-svg-9YEbX3Yhk9BS7cvF .cluster span{color:#333;}#mermaid-svg-9YEbX3Yhk9BS7cvF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-9YEbX3Yhk9BS7cvF .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-9YEbX3Yhk9BS7cvF rect.text{fill:none;stroke-width:0;}#mermaid-svg-9YEbX3Yhk9BS7cvF .icon-shape,#mermaid-svg-9YEbX3Yhk9BS7cvF .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9YEbX3Yhk9BS7cvF .icon-shape p,#mermaid-svg-9YEbX3Yhk9BS7cvF .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-9YEbX3Yhk9BS7cvF .icon-shape .label rect,#mermaid-svg-9YEbX3Yhk9BS7cvF .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9YEbX3Yhk9BS7cvF .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-9YEbX3Yhk9BS7cvF .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-9YEbX3Yhk9BS7cvF :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

两个等待阶段

阶段1等待数据准备如等待网络数据包到达

阶段2将数据从内核拷贝到用户空间

五大I/O模型:

  • 阻塞I/O(Blocking I/O)- BIO
  • 非阻塞I/O(Non-blocking I/O)- NIO
  • I/O多路复用(I/O Multiplexing)- epoll/select
  • 信号驱动I/O(Signal-driven I/O)
  • 异步I/O(Asynchronous I/O)- AIO

  • 2. 快速概览:五大I/O模型对比

    I/O模型第一阶段(等待数据)第二阶段(拷贝到用户空间)整体是否阻塞代表作
    阻塞I/O 阻塞 阻塞 全程阻塞 传统Socket
    非阻塞I/O 轮询(不阻塞) 阻塞 部分阻塞 设置NONBLOCK
    I/O多路复用 阻塞(select/poll) 阻塞 部分阻塞 Nginx/Redis/Netty
    信号驱动I/O 不阻塞(信号通知) 阻塞 部分阻塞 SIGIO信号
    异步I/O 不阻塞 不阻塞 全程不阻塞 Windows IOCP/Linux io_uring

    #mermaid-svg-71ZFVl5Z1qKshj1b{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-71ZFVl5Z1qKshj1b .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-71ZFVl5Z1qKshj1b .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-71ZFVl5Z1qKshj1b .error-icon{fill:#552222;}#mermaid-svg-71ZFVl5Z1qKshj1b .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-71ZFVl5Z1qKshj1b .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-71ZFVl5Z1qKshj1b .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-71ZFVl5Z1qKshj1b .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-71ZFVl5Z1qKshj1b .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-71ZFVl5Z1qKshj1b .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-71ZFVl5Z1qKshj1b .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-71ZFVl5Z1qKshj1b .marker{fill:#333333;stroke:#333333;}#mermaid-svg-71ZFVl5Z1qKshj1b .marker.cross{stroke:#333333;}#mermaid-svg-71ZFVl5Z1qKshj1b svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-71ZFVl5Z1qKshj1b p{margin:0;}#mermaid-svg-71ZFVl5Z1qKshj1b .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-71ZFVl5Z1qKshj1b .cluster-label text{fill:#333;}#mermaid-svg-71ZFVl5Z1qKshj1b .cluster-label span{color:#333;}#mermaid-svg-71ZFVl5Z1qKshj1b .cluster-label span p{background-color:transparent;}#mermaid-svg-71ZFVl5Z1qKshj1b .label text,#mermaid-svg-71ZFVl5Z1qKshj1b span{fill:#333;color:#333;}#mermaid-svg-71ZFVl5Z1qKshj1b .node rect,#mermaid-svg-71ZFVl5Z1qKshj1b .node circle,#mermaid-svg-71ZFVl5Z1qKshj1b .node ellipse,#mermaid-svg-71ZFVl5Z1qKshj1b .node polygon,#mermaid-svg-71ZFVl5Z1qKshj1b .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-71ZFVl5Z1qKshj1b .rough-node .label text,#mermaid-svg-71ZFVl5Z1qKshj1b .node .label text,#mermaid-svg-71ZFVl5Z1qKshj1b .image-shape .label,#mermaid-svg-71ZFVl5Z1qKshj1b .icon-shape .label{text-anchor:middle;}#mermaid-svg-71ZFVl5Z1qKshj1b .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-71ZFVl5Z1qKshj1b .rough-node .label,#mermaid-svg-71ZFVl5Z1qKshj1b .node .label,#mermaid-svg-71ZFVl5Z1qKshj1b .image-shape .label,#mermaid-svg-71ZFVl5Z1qKshj1b .icon-shape .label{text-align:center;}#mermaid-svg-71ZFVl5Z1qKshj1b .node.clickable{cursor:pointer;}#mermaid-svg-71ZFVl5Z1qKshj1b .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-71ZFVl5Z1qKshj1b .arrowheadPath{fill:#333333;}#mermaid-svg-71ZFVl5Z1qKshj1b .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-71ZFVl5Z1qKshj1b .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-71ZFVl5Z1qKshj1b .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-71ZFVl5Z1qKshj1b .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-71ZFVl5Z1qKshj1b .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-71ZFVl5Z1qKshj1b .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-71ZFVl5Z1qKshj1b .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-71ZFVl5Z1qKshj1b .cluster text{fill:#333;}#mermaid-svg-71ZFVl5Z1qKshj1b .cluster span{color:#333;}#mermaid-svg-71ZFVl5Z1qKshj1b div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-71ZFVl5Z1qKshj1b .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-71ZFVl5Z1qKshj1b rect.text{fill:none;stroke-width:0;}#mermaid-svg-71ZFVl5Z1qKshj1b .icon-shape,#mermaid-svg-71ZFVl5Z1qKshj1b .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-71ZFVl5Z1qKshj1b .icon-shape p,#mermaid-svg-71ZFVl5Z1qKshj1b .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-71ZFVl5Z1qKshj1b .icon-shape .label rect,#mermaid-svg-71ZFVl5Z1qKshj1b .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-71ZFVl5Z1qKshj1b .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-71ZFVl5Z1qKshj1b .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-71ZFVl5Z1qKshj1b :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    阻塞程度对比

    BIO 全程阻塞🥵

    NIO 轮询+阻塞😐

    多路复用 并发处理😊

    AIO 全程非阻塞🚀


    3. 模型一:阻塞I/O(BIO)

    3.1 核心流程

    硬件设备

    内核

    应用程序

    硬件设备

    内核

    应用程序

    #mermaid-svg-qLB6nvg0xUOe3wGM{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-qLB6nvg0xUOe3wGM .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-qLB6nvg0xUOe3wGM .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-qLB6nvg0xUOe3wGM .error-icon{fill:#552222;}#mermaid-svg-qLB6nvg0xUOe3wGM .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-qLB6nvg0xUOe3wGM .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-qLB6nvg0xUOe3wGM .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-qLB6nvg0xUOe3wGM .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-qLB6nvg0xUOe3wGM .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-qLB6nvg0xUOe3wGM .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-qLB6nvg0xUOe3wGM .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-qLB6nvg0xUOe3wGM .marker{fill:#333333;stroke:#333333;}#mermaid-svg-qLB6nvg0xUOe3wGM .marker.cross{stroke:#333333;}#mermaid-svg-qLB6nvg0xUOe3wGM svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-qLB6nvg0xUOe3wGM p{margin:0;}#mermaid-svg-qLB6nvg0xUOe3wGM .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-qLB6nvg0xUOe3wGM text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-qLB6nvg0xUOe3wGM .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-qLB6nvg0xUOe3wGM .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-qLB6nvg0xUOe3wGM .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-qLB6nvg0xUOe3wGM .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-qLB6nvg0xUOe3wGM #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-qLB6nvg0xUOe3wGM .sequenceNumber{fill:white;}#mermaid-svg-qLB6nvg0xUOe3wGM #sequencenumber{fill:#333;}#mermaid-svg-qLB6nvg0xUOe3wGM #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-qLB6nvg0xUOe3wGM .messageText{fill:#333;stroke:none;}#mermaid-svg-qLB6nvg0xUOe3wGM .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-qLB6nvg0xUOe3wGM .labelText,#mermaid-svg-qLB6nvg0xUOe3wGM .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-qLB6nvg0xUOe3wGM .loopText,#mermaid-svg-qLB6nvg0xUOe3wGM .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-qLB6nvg0xUOe3wGM .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-qLB6nvg0xUOe3wGM .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-qLB6nvg0xUOe3wGM .noteText,#mermaid-svg-qLB6nvg0xUOe3wGM .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-qLB6nvg0xUOe3wGM .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-qLB6nvg0xUOe3wGM .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-qLB6nvg0xUOe3wGM .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-qLB6nvg0xUOe3wGM .actorPopupMenu{position:absolute;}#mermaid-svg-qLB6nvg0xUOe3wGM .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-qLB6nvg0xUOe3wGM .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-qLB6nvg0xUOe3wGM .actor-man circle,#mermaid-svg-qLB6nvg0xUOe3wGM line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-qLB6nvg0xUOe3wGM :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    第一阶段:等待数据就绪

    第二阶段:拷贝数据

    整个过程中,应用程序完全阻塞

    recvfrom() 系统调用

    等待数据

    数据准备完成

    将数据从内核拷贝到用户空间

    返回成功

    3.2 特点

    #mermaid-svg-S5LyEjO1i4JWqNQD{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-S5LyEjO1i4JWqNQD .error-icon{fill:#552222;}#mermaid-svg-S5LyEjO1i4JWqNQD .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-S5LyEjO1i4JWqNQD .marker{fill:#333333;stroke:#333333;}#mermaid-svg-S5LyEjO1i4JWqNQD .marker.cross{stroke:#333333;}#mermaid-svg-S5LyEjO1i4JWqNQD svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-S5LyEjO1i4JWqNQD p{margin:0;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge{stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .section–1 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section–1 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section–1 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section–1 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section–1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section–1 text{fill:#ffffff;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon–1{font-size:40px;color:#ffffff;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge–1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth–1{stroke-width:17;}#mermaid-svg-S5LyEjO1i4JWqNQD .section–1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-0 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-0 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-0 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-0 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-0 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-0{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-0{stroke-width:14;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-1 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-1 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-1 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-1 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-1 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-1{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-1{stroke-width:11;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-2 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-2 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-2 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-2 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-2 text{fill:#ffffff;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-2{stroke-width:8;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-3 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-3 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-3 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-3 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-3 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-3{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-3{stroke-width:5;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-4 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-4 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-4 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-4 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-4 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-4{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-4{stroke-width:2;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-5 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-5 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-5 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-5 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-5 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-5{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-5{stroke-width:-1;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-6 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-6 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-6 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-6 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-6 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-6{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-6{stroke-width:-4;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-7 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-7 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-7 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-7 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-7 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-7{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-7{stroke-width:-7;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-8 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-8 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-8 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-8 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-8 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-8{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-8{stroke-width:-10;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-9 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-9 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-9 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-9 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-9 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-9{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-9{stroke-width:-13;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-10 rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-10 path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-10 circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-10 polygon,#mermaid-svg-S5LyEjO1i4JWqNQD .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-10 text{fill:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .node-icon-10{font-size:40px;color:black;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .edge-depth-10{stroke-width:-16;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled circle,#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:lightgray;}#mermaid-svg-S5LyEjO1i4JWqNQD .disabled text{fill:#efefef;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-root rect,#mermaid-svg-S5LyEjO1i4JWqNQD .section-root path,#mermaid-svg-S5LyEjO1i4JWqNQD .section-root circle,#mermaid-svg-S5LyEjO1i4JWqNQD .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-S5LyEjO1i4JWqNQD .section-root text{fill:#ffffff;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-root span{color:#ffffff;}#mermaid-svg-S5LyEjO1i4JWqNQD .section-2 span{color:#ffffff;}#mermaid-svg-S5LyEjO1i4JWqNQD .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-S5LyEjO1i4JWqNQD .edge{fill:none;}#mermaid-svg-S5LyEjO1i4JWqNQD .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-S5LyEjO1i4JWqNQD :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    阻塞I/O BIO

    优点

    使用简单

    开发调试方便

    内核自动调度

    缺点

    一个线程处理一个连接

    线程上下文切换开销大

    无法支撑高并发

    典型场景

    JDBC数据库连接

    低并发业务

    简单Demo

    3.3 Java代码示例

    // 传统BIO服务端 – 每个连接一个线程
    public class BioServer {
    public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(8080);

    while (true) {
    // 此处阻塞,等待客户端连接
    Socket socket = serverSocket.accept();

    // 为每个连接创建新线程
    new Thread(() -> {
    try {
    BufferedReader reader = new BufferedReader(
    new InputStreamReader(socket.getInputStream())
    );
    String line;
    // 此处阻塞,等待数据读取
    while ((line = reader.readLine()) != null) {
    System.out.println(line);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }).start();
    }
    }
    }


    4. 模型二:非阻塞I/O(NIO)

    4.1 核心流程

    内核

    应用程序

    内核

    应用程序

    #mermaid-svg-PJkKGVzQlmEPuUTz{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-PJkKGVzQlmEPuUTz .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-PJkKGVzQlmEPuUTz .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-PJkKGVzQlmEPuUTz .error-icon{fill:#552222;}#mermaid-svg-PJkKGVzQlmEPuUTz .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-PJkKGVzQlmEPuUTz .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-PJkKGVzQlmEPuUTz .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-PJkKGVzQlmEPuUTz .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-PJkKGVzQlmEPuUTz .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-PJkKGVzQlmEPuUTz .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-PJkKGVzQlmEPuUTz .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-PJkKGVzQlmEPuUTz .marker{fill:#333333;stroke:#333333;}#mermaid-svg-PJkKGVzQlmEPuUTz .marker.cross{stroke:#333333;}#mermaid-svg-PJkKGVzQlmEPuUTz svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-PJkKGVzQlmEPuUTz p{margin:0;}#mermaid-svg-PJkKGVzQlmEPuUTz .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-PJkKGVzQlmEPuUTz text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-PJkKGVzQlmEPuUTz .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-PJkKGVzQlmEPuUTz .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-PJkKGVzQlmEPuUTz .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-PJkKGVzQlmEPuUTz .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-PJkKGVzQlmEPuUTz #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-PJkKGVzQlmEPuUTz .sequenceNumber{fill:white;}#mermaid-svg-PJkKGVzQlmEPuUTz #sequencenumber{fill:#333;}#mermaid-svg-PJkKGVzQlmEPuUTz #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-PJkKGVzQlmEPuUTz .messageText{fill:#333;stroke:none;}#mermaid-svg-PJkKGVzQlmEPuUTz .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-PJkKGVzQlmEPuUTz .labelText,#mermaid-svg-PJkKGVzQlmEPuUTz .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-PJkKGVzQlmEPuUTz .loopText,#mermaid-svg-PJkKGVzQlmEPuUTz .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-PJkKGVzQlmEPuUTz .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-PJkKGVzQlmEPuUTz .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-PJkKGVzQlmEPuUTz .noteText,#mermaid-svg-PJkKGVzQlmEPuUTz .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-PJkKGVzQlmEPuUTz .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-PJkKGVzQlmEPuUTz .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-PJkKGVzQlmEPuUTz .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-PJkKGVzQlmEPuUTz .actorPopupMenu{position:absolute;}#mermaid-svg-PJkKGVzQlmEPuUTz .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-PJkKGVzQlmEPuUTz .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-PJkKGVzQlmEPuUTz .actor-man circle,#mermaid-svg-PJkKGVzQlmEPuUTz line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-PJkKGVzQlmEPuUTz :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    数据未就绪

    loop

    [轮询循环]

    数据准备就绪

    轮询过程中不阻塞,

    但拷贝数据时仍阻塞

    recvfrom() 系统调用

    返回EWOULDBLOCK错误

    继续其他任务或等待

    recvfrom() 系统调用

    拷贝数据到用户空间

    返回成功

    4.2 非阻塞I/O流程图

    #mermaid-svg-ZAQ9sThPIkso0cas{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ZAQ9sThPIkso0cas .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ZAQ9sThPIkso0cas .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ZAQ9sThPIkso0cas .error-icon{fill:#552222;}#mermaid-svg-ZAQ9sThPIkso0cas .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZAQ9sThPIkso0cas .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ZAQ9sThPIkso0cas .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZAQ9sThPIkso0cas .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZAQ9sThPIkso0cas .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ZAQ9sThPIkso0cas .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZAQ9sThPIkso0cas .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZAQ9sThPIkso0cas .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZAQ9sThPIkso0cas .marker.cross{stroke:#333333;}#mermaid-svg-ZAQ9sThPIkso0cas svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZAQ9sThPIkso0cas p{margin:0;}#mermaid-svg-ZAQ9sThPIkso0cas .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-ZAQ9sThPIkso0cas .cluster-label text{fill:#333;}#mermaid-svg-ZAQ9sThPIkso0cas .cluster-label span{color:#333;}#mermaid-svg-ZAQ9sThPIkso0cas .cluster-label span p{background-color:transparent;}#mermaid-svg-ZAQ9sThPIkso0cas .label text,#mermaid-svg-ZAQ9sThPIkso0cas span{fill:#333;color:#333;}#mermaid-svg-ZAQ9sThPIkso0cas .node rect,#mermaid-svg-ZAQ9sThPIkso0cas .node circle,#mermaid-svg-ZAQ9sThPIkso0cas .node ellipse,#mermaid-svg-ZAQ9sThPIkso0cas .node polygon,#mermaid-svg-ZAQ9sThPIkso0cas .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZAQ9sThPIkso0cas .rough-node .label text,#mermaid-svg-ZAQ9sThPIkso0cas .node .label text,#mermaid-svg-ZAQ9sThPIkso0cas .image-shape .label,#mermaid-svg-ZAQ9sThPIkso0cas .icon-shape .label{text-anchor:middle;}#mermaid-svg-ZAQ9sThPIkso0cas .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ZAQ9sThPIkso0cas .rough-node .label,#mermaid-svg-ZAQ9sThPIkso0cas .node .label,#mermaid-svg-ZAQ9sThPIkso0cas .image-shape .label,#mermaid-svg-ZAQ9sThPIkso0cas .icon-shape .label{text-align:center;}#mermaid-svg-ZAQ9sThPIkso0cas .node.clickable{cursor:pointer;}#mermaid-svg-ZAQ9sThPIkso0cas .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ZAQ9sThPIkso0cas .arrowheadPath{fill:#333333;}#mermaid-svg-ZAQ9sThPIkso0cas .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ZAQ9sThPIkso0cas .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ZAQ9sThPIkso0cas .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZAQ9sThPIkso0cas .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ZAQ9sThPIkso0cas .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZAQ9sThPIkso0cas .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ZAQ9sThPIkso0cas .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZAQ9sThPIkso0cas .cluster text{fill:#333;}#mermaid-svg-ZAQ9sThPIkso0cas .cluster span{color:#333;}#mermaid-svg-ZAQ9sThPIkso0cas div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ZAQ9sThPIkso0cas .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ZAQ9sThPIkso0cas rect.text{fill:none;stroke-width:0;}#mermaid-svg-ZAQ9sThPIkso0cas .icon-shape,#mermaid-svg-ZAQ9sThPIkso0cas .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ZAQ9sThPIkso0cas .icon-shape p,#mermaid-svg-ZAQ9sThPIkso0cas .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ZAQ9sThPIkso0cas .icon-shape .label rect,#mermaid-svg-ZAQ9sThPIkso0cas .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ZAQ9sThPIkso0cas .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ZAQ9sThPIkso0cas .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ZAQ9sThPIkso0cas :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    应用程序发起recvfrom

    数据是否就绪?

    立即返回EWOULDBLOCK

    短暂延时/做其他事

    内核拷贝数据到用户空间

    返回成功

    4.3 特点

    #mermaid-svg-rf92apTdLDg1H5Zg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-rf92apTdLDg1H5Zg .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-rf92apTdLDg1H5Zg .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-rf92apTdLDg1H5Zg .error-icon{fill:#552222;}#mermaid-svg-rf92apTdLDg1H5Zg .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-rf92apTdLDg1H5Zg .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-rf92apTdLDg1H5Zg .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-rf92apTdLDg1H5Zg .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-rf92apTdLDg1H5Zg .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-rf92apTdLDg1H5Zg .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-rf92apTdLDg1H5Zg .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-rf92apTdLDg1H5Zg .marker{fill:#333333;stroke:#333333;}#mermaid-svg-rf92apTdLDg1H5Zg .marker.cross{stroke:#333333;}#mermaid-svg-rf92apTdLDg1H5Zg svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-rf92apTdLDg1H5Zg p{margin:0;}#mermaid-svg-rf92apTdLDg1H5Zg .edge{stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .section–1 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section–1 path,#mermaid-svg-rf92apTdLDg1H5Zg .section–1 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section–1 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section–1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section–1 text{fill:#ffffff;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon–1{font-size:40px;color:#ffffff;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge–1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth–1{stroke-width:17;}#mermaid-svg-rf92apTdLDg1H5Zg .section–1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-0 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-0 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-0 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-0 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-0 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-0{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-0{stroke-width:14;}#mermaid-svg-rf92apTdLDg1H5Zg .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-1 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-1 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-1 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-1 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-1 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-1{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-1{stroke-width:11;}#mermaid-svg-rf92apTdLDg1H5Zg .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-2 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-2 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-2 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-2 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-2 text{fill:#ffffff;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-2{stroke-width:8;}#mermaid-svg-rf92apTdLDg1H5Zg .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-3 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-3 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-3 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-3 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-3 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-3{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-3{stroke-width:5;}#mermaid-svg-rf92apTdLDg1H5Zg .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-4 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-4 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-4 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-4 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-4 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-4{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-4{stroke-width:2;}#mermaid-svg-rf92apTdLDg1H5Zg .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-5 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-5 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-5 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-5 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-5 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-5{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-5{stroke-width:-1;}#mermaid-svg-rf92apTdLDg1H5Zg .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-6 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-6 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-6 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-6 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-6 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-6{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-6{stroke-width:-4;}#mermaid-svg-rf92apTdLDg1H5Zg .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-7 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-7 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-7 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-7 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-7 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-7{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-7{stroke-width:-7;}#mermaid-svg-rf92apTdLDg1H5Zg .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-8 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-8 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-8 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-8 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-8 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-8{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-8{stroke-width:-10;}#mermaid-svg-rf92apTdLDg1H5Zg .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-9 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-9 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-9 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-9 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-9 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-9{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-9{stroke-width:-13;}#mermaid-svg-rf92apTdLDg1H5Zg .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-10 rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-10 path,#mermaid-svg-rf92apTdLDg1H5Zg .section-10 circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-10 polygon,#mermaid-svg-rf92apTdLDg1H5Zg .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-10 text{fill:black;}#mermaid-svg-rf92apTdLDg1H5Zg .node-icon-10{font-size:40px;color:black;}#mermaid-svg-rf92apTdLDg1H5Zg .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .edge-depth-10{stroke-width:-16;}#mermaid-svg-rf92apTdLDg1H5Zg .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled,#mermaid-svg-rf92apTdLDg1H5Zg .disabled circle,#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:lightgray;}#mermaid-svg-rf92apTdLDg1H5Zg .disabled text{fill:#efefef;}#mermaid-svg-rf92apTdLDg1H5Zg .section-root rect,#mermaid-svg-rf92apTdLDg1H5Zg .section-root path,#mermaid-svg-rf92apTdLDg1H5Zg .section-root circle,#mermaid-svg-rf92apTdLDg1H5Zg .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-rf92apTdLDg1H5Zg .section-root text{fill:#ffffff;}#mermaid-svg-rf92apTdLDg1H5Zg .section-root span{color:#ffffff;}#mermaid-svg-rf92apTdLDg1H5Zg .section-2 span{color:#ffffff;}#mermaid-svg-rf92apTdLDg1H5Zg .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-rf92apTdLDg1H5Zg .edge{fill:none;}#mermaid-svg-rf92apTdLDg1H5Zg .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-rf92apTdLDg1H5Zg :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    非阻塞I/O NIO

    优点

    调用不会阻塞线程

    一个线程可管理多个连接

    提高CPU利用率

    缺点

    轮询占用CPU

    频繁系统调用

    实现复杂

    轮询问题

    空轮询造成CPU浪费

    需要设置合理延时


    5. 模型三:I/O多路复用

    5.1 核心流程

    内核

    应用程序

    内核

    应用程序

    #mermaid-svg-AcghjHNQ8EXASjSU{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-AcghjHNQ8EXASjSU .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-AcghjHNQ8EXASjSU .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-AcghjHNQ8EXASjSU .error-icon{fill:#552222;}#mermaid-svg-AcghjHNQ8EXASjSU .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-AcghjHNQ8EXASjSU .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-AcghjHNQ8EXASjSU .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-AcghjHNQ8EXASjSU .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-AcghjHNQ8EXASjSU .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-AcghjHNQ8EXASjSU .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-AcghjHNQ8EXASjSU .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-AcghjHNQ8EXASjSU .marker{fill:#333333;stroke:#333333;}#mermaid-svg-AcghjHNQ8EXASjSU .marker.cross{stroke:#333333;}#mermaid-svg-AcghjHNQ8EXASjSU svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-AcghjHNQ8EXASjSU p{margin:0;}#mermaid-svg-AcghjHNQ8EXASjSU .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-AcghjHNQ8EXASjSU text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-AcghjHNQ8EXASjSU .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-AcghjHNQ8EXASjSU .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-AcghjHNQ8EXASjSU .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-AcghjHNQ8EXASjSU .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-AcghjHNQ8EXASjSU #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-AcghjHNQ8EXASjSU .sequenceNumber{fill:white;}#mermaid-svg-AcghjHNQ8EXASjSU #sequencenumber{fill:#333;}#mermaid-svg-AcghjHNQ8EXASjSU #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-AcghjHNQ8EXASjSU .messageText{fill:#333;stroke:none;}#mermaid-svg-AcghjHNQ8EXASjSU .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-AcghjHNQ8EXASjSU .labelText,#mermaid-svg-AcghjHNQ8EXASjSU .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-AcghjHNQ8EXASjSU .loopText,#mermaid-svg-AcghjHNQ8EXASjSU .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-AcghjHNQ8EXASjSU .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-AcghjHNQ8EXASjSU .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-AcghjHNQ8EXASjSU .noteText,#mermaid-svg-AcghjHNQ8EXASjSU .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-AcghjHNQ8EXASjSU .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-AcghjHNQ8EXASjSU .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-AcghjHNQ8EXASjSU .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-AcghjHNQ8EXASjSU .actorPopupMenu{position:absolute;}#mermaid-svg-AcghjHNQ8EXASjSU .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-AcghjHNQ8EXASjSU .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-AcghjHNQ8EXASjSU .actor-man circle,#mermaid-svg-AcghjHNQ8EXASjSU line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-AcghjHNQ8EXASjSU :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    使用select/poll/epoll

    监听多个文件描述符

    select() 系统调用

    返回有数据就绪的fd

    recvfrom() 系统调用

    拷贝数据

    返回成功

    5.2 多路复用核心思想

    #mermaid-svg-CCutwXOr2Cgoda9J{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-CCutwXOr2Cgoda9J .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-CCutwXOr2Cgoda9J .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-CCutwXOr2Cgoda9J .error-icon{fill:#552222;}#mermaid-svg-CCutwXOr2Cgoda9J .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-CCutwXOr2Cgoda9J .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-CCutwXOr2Cgoda9J .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-CCutwXOr2Cgoda9J .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-CCutwXOr2Cgoda9J .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-CCutwXOr2Cgoda9J .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-CCutwXOr2Cgoda9J .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-CCutwXOr2Cgoda9J .marker{fill:#333333;stroke:#333333;}#mermaid-svg-CCutwXOr2Cgoda9J .marker.cross{stroke:#333333;}#mermaid-svg-CCutwXOr2Cgoda9J svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-CCutwXOr2Cgoda9J p{margin:0;}#mermaid-svg-CCutwXOr2Cgoda9J .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-CCutwXOr2Cgoda9J .cluster-label text{fill:#333;}#mermaid-svg-CCutwXOr2Cgoda9J .cluster-label span{color:#333;}#mermaid-svg-CCutwXOr2Cgoda9J .cluster-label span p{background-color:transparent;}#mermaid-svg-CCutwXOr2Cgoda9J .label text,#mermaid-svg-CCutwXOr2Cgoda9J span{fill:#333;color:#333;}#mermaid-svg-CCutwXOr2Cgoda9J .node rect,#mermaid-svg-CCutwXOr2Cgoda9J .node circle,#mermaid-svg-CCutwXOr2Cgoda9J .node ellipse,#mermaid-svg-CCutwXOr2Cgoda9J .node polygon,#mermaid-svg-CCutwXOr2Cgoda9J .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-CCutwXOr2Cgoda9J .rough-node .label text,#mermaid-svg-CCutwXOr2Cgoda9J .node .label text,#mermaid-svg-CCutwXOr2Cgoda9J .image-shape .label,#mermaid-svg-CCutwXOr2Cgoda9J .icon-shape .label{text-anchor:middle;}#mermaid-svg-CCutwXOr2Cgoda9J .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-CCutwXOr2Cgoda9J .rough-node .label,#mermaid-svg-CCutwXOr2Cgoda9J .node .label,#mermaid-svg-CCutwXOr2Cgoda9J .image-shape .label,#mermaid-svg-CCutwXOr2Cgoda9J .icon-shape .label{text-align:center;}#mermaid-svg-CCutwXOr2Cgoda9J .node.clickable{cursor:pointer;}#mermaid-svg-CCutwXOr2Cgoda9J .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-CCutwXOr2Cgoda9J .arrowheadPath{fill:#333333;}#mermaid-svg-CCutwXOr2Cgoda9J .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-CCutwXOr2Cgoda9J .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-CCutwXOr2Cgoda9J .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-CCutwXOr2Cgoda9J .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-CCutwXOr2Cgoda9J .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-CCutwXOr2Cgoda9J .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-CCutwXOr2Cgoda9J .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-CCutwXOr2Cgoda9J .cluster text{fill:#333;}#mermaid-svg-CCutwXOr2Cgoda9J .cluster span{color:#333;}#mermaid-svg-CCutwXOr2Cgoda9J div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-CCutwXOr2Cgoda9J .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-CCutwXOr2Cgoda9J rect.text{fill:none;stroke-width:0;}#mermaid-svg-CCutwXOr2Cgoda9J .icon-shape,#mermaid-svg-CCutwXOr2Cgoda9J .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-CCutwXOr2Cgoda9J .icon-shape p,#mermaid-svg-CCutwXOr2Cgoda9J .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-CCutwXOr2Cgoda9J .icon-shape .label rect,#mermaid-svg-CCutwXOr2Cgoda9J .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-CCutwXOr2Cgoda9J .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-CCutwXOr2Cgoda9J .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-CCutwXOr2Cgoda9J :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    I/O多路复用

    监听

    监听

    监听

    监听

    单个线程/进程Selector

    Socket1

    Socket2

    Socket3

    传统BIO

    线程1

    Socket1

    线程2

    Socket2

    线程3

    Socket3

    5.3 select/poll/epoll 演进

    #mermaid-svg-TYIjO6ViOAtTmQHg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-TYIjO6ViOAtTmQHg .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-TYIjO6ViOAtTmQHg .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-TYIjO6ViOAtTmQHg .error-icon{fill:#552222;}#mermaid-svg-TYIjO6ViOAtTmQHg .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-TYIjO6ViOAtTmQHg .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-TYIjO6ViOAtTmQHg .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-TYIjO6ViOAtTmQHg .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-TYIjO6ViOAtTmQHg .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-TYIjO6ViOAtTmQHg .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-TYIjO6ViOAtTmQHg .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-TYIjO6ViOAtTmQHg .marker{fill:#333333;stroke:#333333;}#mermaid-svg-TYIjO6ViOAtTmQHg .marker.cross{stroke:#333333;}#mermaid-svg-TYIjO6ViOAtTmQHg svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-TYIjO6ViOAtTmQHg p{margin:0;}#mermaid-svg-TYIjO6ViOAtTmQHg .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-TYIjO6ViOAtTmQHg .cluster-label text{fill:#333;}#mermaid-svg-TYIjO6ViOAtTmQHg .cluster-label span{color:#333;}#mermaid-svg-TYIjO6ViOAtTmQHg .cluster-label span p{background-color:transparent;}#mermaid-svg-TYIjO6ViOAtTmQHg .label text,#mermaid-svg-TYIjO6ViOAtTmQHg span{fill:#333;color:#333;}#mermaid-svg-TYIjO6ViOAtTmQHg .node rect,#mermaid-svg-TYIjO6ViOAtTmQHg .node circle,#mermaid-svg-TYIjO6ViOAtTmQHg .node ellipse,#mermaid-svg-TYIjO6ViOAtTmQHg .node polygon,#mermaid-svg-TYIjO6ViOAtTmQHg .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-TYIjO6ViOAtTmQHg .rough-node .label text,#mermaid-svg-TYIjO6ViOAtTmQHg .node .label text,#mermaid-svg-TYIjO6ViOAtTmQHg .image-shape .label,#mermaid-svg-TYIjO6ViOAtTmQHg .icon-shape .label{text-anchor:middle;}#mermaid-svg-TYIjO6ViOAtTmQHg .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-TYIjO6ViOAtTmQHg .rough-node .label,#mermaid-svg-TYIjO6ViOAtTmQHg .node .label,#mermaid-svg-TYIjO6ViOAtTmQHg .image-shape .label,#mermaid-svg-TYIjO6ViOAtTmQHg .icon-shape .label{text-align:center;}#mermaid-svg-TYIjO6ViOAtTmQHg .node.clickable{cursor:pointer;}#mermaid-svg-TYIjO6ViOAtTmQHg .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-TYIjO6ViOAtTmQHg .arrowheadPath{fill:#333333;}#mermaid-svg-TYIjO6ViOAtTmQHg .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-TYIjO6ViOAtTmQHg .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-TYIjO6ViOAtTmQHg .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TYIjO6ViOAtTmQHg .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-TYIjO6ViOAtTmQHg .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TYIjO6ViOAtTmQHg .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-TYIjO6ViOAtTmQHg .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-TYIjO6ViOAtTmQHg .cluster text{fill:#333;}#mermaid-svg-TYIjO6ViOAtTmQHg .cluster span{color:#333;}#mermaid-svg-TYIjO6ViOAtTmQHg div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-TYIjO6ViOAtTmQHg .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-TYIjO6ViOAtTmQHg rect.text{fill:none;stroke-width:0;}#mermaid-svg-TYIjO6ViOAtTmQHg .icon-shape,#mermaid-svg-TYIjO6ViOAtTmQHg .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-TYIjO6ViOAtTmQHg .icon-shape p,#mermaid-svg-TYIjO6ViOAtTmQHg .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-TYIjO6ViOAtTmQHg .icon-shape .label rect,#mermaid-svg-TYIjO6ViOAtTmQHg .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-TYIjO6ViOAtTmQHg .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-TYIjO6ViOAtTmQHg .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-TYIjO6ViOAtTmQHg :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    epoll

    事件驱动

    只返回就绪fd

    O1复杂度

    红黑树+就绪链表

    poll

    链表结构无上限

    仍需拷贝全部fd

    O遍历查找

    select

    描述符集合FD_SETSIZE限制1024

    每次拷贝全部fd

    O遍历查找就绪fd

    5.4 多路复用工作流程

    渲染错误: Mermaid 渲染失败: Parse error on line 3: …LOOP{selector.select()} LOOP –>|有就绪 ———————–^ Expecting 'SQE', 'DOUBLECIRCLEEND', 'PE', '-)', 'STADIUMEND', 'SUBROUTINEEND', 'PIPE', 'CYLINDEREND', 'DIAMOND_STOP', 'TAGEND', 'TRAPEND', 'INVTRAPEND', 'UNICODE_TEXT', 'TEXT', 'TAGSTART', got 'PS'

    5.5 Java NIO代码示例

    // NIO多路复用服务端
    public class NioServer {
    public static void main(String[] args) throws IOException {
    Selector selector = Selector.open();
    ServerSocketChannel serverChannel = ServerSocketChannel.open();
    serverChannel.bind(new InetSocketAddress(8080));
    serverChannel.configureBlocking(false);
    serverChannel.register(selector, SelectionKey.OP_ACCEPT);

    while (true) {
    // 阻塞,等待就绪事件
    selector.select();

    Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
    while (iterator.hasNext()) {
    SelectionKey key = iterator.next();
    iterator.remove();

    if (key.isAcceptable()) {
    SocketChannel client = serverChannel.accept();
    client.configureBlocking(false);
    client.register(selector, SelectionKey.OP_READ);
    } else if (key.isReadable()) {
    SocketChannel client = (SocketChannel) key.channel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    client.read(buffer); // 数据已就绪,不阻塞
    // 处理数据…
    }
    }
    }
    }
    }


    6. 模型四:信号驱动I/O

    6.1 核心流程

    内核

    应用程序

    内核

    应用程序

    #mermaid-svg-oikj8EUYAERE9qwF{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-oikj8EUYAERE9qwF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-oikj8EUYAERE9qwF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-oikj8EUYAERE9qwF .error-icon{fill:#552222;}#mermaid-svg-oikj8EUYAERE9qwF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-oikj8EUYAERE9qwF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-oikj8EUYAERE9qwF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-oikj8EUYAERE9qwF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-oikj8EUYAERE9qwF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-oikj8EUYAERE9qwF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-oikj8EUYAERE9qwF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-oikj8EUYAERE9qwF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-oikj8EUYAERE9qwF .marker.cross{stroke:#333333;}#mermaid-svg-oikj8EUYAERE9qwF svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-oikj8EUYAERE9qwF p{margin:0;}#mermaid-svg-oikj8EUYAERE9qwF .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-oikj8EUYAERE9qwF text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-oikj8EUYAERE9qwF .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-oikj8EUYAERE9qwF .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-oikj8EUYAERE9qwF .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-oikj8EUYAERE9qwF .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-oikj8EUYAERE9qwF #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-oikj8EUYAERE9qwF .sequenceNumber{fill:white;}#mermaid-svg-oikj8EUYAERE9qwF #sequencenumber{fill:#333;}#mermaid-svg-oikj8EUYAERE9qwF #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-oikj8EUYAERE9qwF .messageText{fill:#333;stroke:none;}#mermaid-svg-oikj8EUYAERE9qwF .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-oikj8EUYAERE9qwF .labelText,#mermaid-svg-oikj8EUYAERE9qwF .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-oikj8EUYAERE9qwF .loopText,#mermaid-svg-oikj8EUYAERE9qwF .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-oikj8EUYAERE9qwF .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-oikj8EUYAERE9qwF .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-oikj8EUYAERE9qwF .noteText,#mermaid-svg-oikj8EUYAERE9qwF .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-oikj8EUYAERE9qwF .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-oikj8EUYAERE9qwF .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-oikj8EUYAERE9qwF .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-oikj8EUYAERE9qwF .actorPopupMenu{position:absolute;}#mermaid-svg-oikj8EUYAERE9qwF .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-oikj8EUYAERE9qwF .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-oikj8EUYAERE9qwF .actor-man circle,#mermaid-svg-oikj8EUYAERE9qwF line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-oikj8EUYAERE9qwF :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    立即返回,数据未就绪

    数据准备就绪

    sigaction() 注册信号处理函数

    recvfrom() 系统调用

    等待数据准备

    发送SIGIO信号

    recvfrom() 系统调用

    拷贝数据到用户空间

    返回成功

    6.2 特点

    #mermaid-svg-viPKKhHkVlwpc19u{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-viPKKhHkVlwpc19u .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-viPKKhHkVlwpc19u .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-viPKKhHkVlwpc19u .error-icon{fill:#552222;}#mermaid-svg-viPKKhHkVlwpc19u .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-viPKKhHkVlwpc19u .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-viPKKhHkVlwpc19u .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-viPKKhHkVlwpc19u .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-viPKKhHkVlwpc19u .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-viPKKhHkVlwpc19u .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-viPKKhHkVlwpc19u .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-viPKKhHkVlwpc19u .marker{fill:#333333;stroke:#333333;}#mermaid-svg-viPKKhHkVlwpc19u .marker.cross{stroke:#333333;}#mermaid-svg-viPKKhHkVlwpc19u svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-viPKKhHkVlwpc19u p{margin:0;}#mermaid-svg-viPKKhHkVlwpc19u .edge{stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .section–1 rect,#mermaid-svg-viPKKhHkVlwpc19u .section–1 path,#mermaid-svg-viPKKhHkVlwpc19u .section–1 circle,#mermaid-svg-viPKKhHkVlwpc19u .section–1 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section–1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section–1 text{fill:#ffffff;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon–1{font-size:40px;color:#ffffff;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge–1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth–1{stroke-width:17;}#mermaid-svg-viPKKhHkVlwpc19u .section–1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-0 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-0 path,#mermaid-svg-viPKKhHkVlwpc19u .section-0 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-0 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-viPKKhHkVlwpc19u .section-0 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-0{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-0{stroke-width:14;}#mermaid-svg-viPKKhHkVlwpc19u .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-1 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-1 path,#mermaid-svg-viPKKhHkVlwpc19u .section-1 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-1 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-1 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-1{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-1{stroke-width:11;}#mermaid-svg-viPKKhHkVlwpc19u .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-2 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-2 path,#mermaid-svg-viPKKhHkVlwpc19u .section-2 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-2 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-2 text{fill:#ffffff;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-2{stroke-width:8;}#mermaid-svg-viPKKhHkVlwpc19u .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-3 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-3 path,#mermaid-svg-viPKKhHkVlwpc19u .section-3 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-3 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-3 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-3{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-3{stroke-width:5;}#mermaid-svg-viPKKhHkVlwpc19u .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-4 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-4 path,#mermaid-svg-viPKKhHkVlwpc19u .section-4 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-4 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-4 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-4{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-4{stroke-width:2;}#mermaid-svg-viPKKhHkVlwpc19u .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-5 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-5 path,#mermaid-svg-viPKKhHkVlwpc19u .section-5 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-5 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-5 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-5{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-5{stroke-width:-1;}#mermaid-svg-viPKKhHkVlwpc19u .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-6 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-6 path,#mermaid-svg-viPKKhHkVlwpc19u .section-6 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-6 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-6 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-6{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-6{stroke-width:-4;}#mermaid-svg-viPKKhHkVlwpc19u .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-7 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-7 path,#mermaid-svg-viPKKhHkVlwpc19u .section-7 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-7 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-7 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-7{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-7{stroke-width:-7;}#mermaid-svg-viPKKhHkVlwpc19u .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-8 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-8 path,#mermaid-svg-viPKKhHkVlwpc19u .section-8 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-8 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-8 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-8{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-8{stroke-width:-10;}#mermaid-svg-viPKKhHkVlwpc19u .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-9 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-9 path,#mermaid-svg-viPKKhHkVlwpc19u .section-9 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-9 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-9 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-9{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-9{stroke-width:-13;}#mermaid-svg-viPKKhHkVlwpc19u .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-10 rect,#mermaid-svg-viPKKhHkVlwpc19u .section-10 path,#mermaid-svg-viPKKhHkVlwpc19u .section-10 circle,#mermaid-svg-viPKKhHkVlwpc19u .section-10 polygon,#mermaid-svg-viPKKhHkVlwpc19u .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-10 text{fill:black;}#mermaid-svg-viPKKhHkVlwpc19u .node-icon-10{font-size:40px;color:black;}#mermaid-svg-viPKKhHkVlwpc19u .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .edge-depth-10{stroke-width:-16;}#mermaid-svg-viPKKhHkVlwpc19u .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-viPKKhHkVlwpc19u .disabled,#mermaid-svg-viPKKhHkVlwpc19u .disabled circle,#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:lightgray;}#mermaid-svg-viPKKhHkVlwpc19u .disabled text{fill:#efefef;}#mermaid-svg-viPKKhHkVlwpc19u .section-root rect,#mermaid-svg-viPKKhHkVlwpc19u .section-root path,#mermaid-svg-viPKKhHkVlwpc19u .section-root circle,#mermaid-svg-viPKKhHkVlwpc19u .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-viPKKhHkVlwpc19u .section-root text{fill:#ffffff;}#mermaid-svg-viPKKhHkVlwpc19u .section-root span{color:#ffffff;}#mermaid-svg-viPKKhHkVlwpc19u .section-2 span{color:#ffffff;}#mermaid-svg-viPKKhHkVlwpc19u .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-viPKKhHkVlwpc19u .edge{fill:none;}#mermaid-svg-viPKKhHkVlwpc19u .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-viPKKhHkVlwpc19u :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    信号驱动I/O

    流程

    注册信号处理函数

    发起非阻塞调用

    信号通知数据就绪

    再次调用读取数据

    优点

    等待数据时不阻塞

    无需轮询

    缺点

    信号队列可能溢出

    信号处理上下文受限

    实际使用较少


    7. 模型五:异步I/O(AIO)

    7.1 核心流程

    内核

    应用程序

    内核

    应用程序

    #mermaid-svg-BeqiGkwBSmbQ4dl3{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-BeqiGkwBSmbQ4dl3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .error-icon{fill:#552222;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .marker.cross{stroke:#333333;}#mermaid-svg-BeqiGkwBSmbQ4dl3 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-BeqiGkwBSmbQ4dl3 p{margin:0;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BeqiGkwBSmbQ4dl3 text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-BeqiGkwBSmbQ4dl3 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-BeqiGkwBSmbQ4dl3 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .sequenceNumber{fill:white;}#mermaid-svg-BeqiGkwBSmbQ4dl3 #sequencenumber{fill:#333;}#mermaid-svg-BeqiGkwBSmbQ4dl3 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .messageText{fill:#333;stroke:none;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .labelText,#mermaid-svg-BeqiGkwBSmbQ4dl3 .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .loopText,#mermaid-svg-BeqiGkwBSmbQ4dl3 .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-BeqiGkwBSmbQ4dl3 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .noteText,#mermaid-svg-BeqiGkwBSmbQ4dl3 .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .actorPopupMenu{position:absolute;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-BeqiGkwBSmbQ4dl3 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BeqiGkwBSmbQ4dl3 .actor-man circle,#mermaid-svg-BeqiGkwBSmbQ4dl3 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-BeqiGkwBSmbQ4dl3 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    立即返回

    数据已在用户空间

    可直接使用

    aio_read() 系统调用

    等待数据准备

    拷贝数据到用户空间

    (应用程序指定的缓冲区)

    发送信号或回调

    7.2 异步I/O vs 其他模型

    #mermaid-svg-tmkXzCd2Gqp93YGm{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-tmkXzCd2Gqp93YGm .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-tmkXzCd2Gqp93YGm .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-tmkXzCd2Gqp93YGm .error-icon{fill:#552222;}#mermaid-svg-tmkXzCd2Gqp93YGm .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-tmkXzCd2Gqp93YGm .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-tmkXzCd2Gqp93YGm .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-tmkXzCd2Gqp93YGm .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-tmkXzCd2Gqp93YGm .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-tmkXzCd2Gqp93YGm .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-tmkXzCd2Gqp93YGm .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-tmkXzCd2Gqp93YGm .marker{fill:#333333;stroke:#333333;}#mermaid-svg-tmkXzCd2Gqp93YGm .marker.cross{stroke:#333333;}#mermaid-svg-tmkXzCd2Gqp93YGm svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-tmkXzCd2Gqp93YGm p{margin:0;}#mermaid-svg-tmkXzCd2Gqp93YGm .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-tmkXzCd2Gqp93YGm .cluster-label text{fill:#333;}#mermaid-svg-tmkXzCd2Gqp93YGm .cluster-label span{color:#333;}#mermaid-svg-tmkXzCd2Gqp93YGm .cluster-label span p{background-color:transparent;}#mermaid-svg-tmkXzCd2Gqp93YGm .label text,#mermaid-svg-tmkXzCd2Gqp93YGm span{fill:#333;color:#333;}#mermaid-svg-tmkXzCd2Gqp93YGm .node rect,#mermaid-svg-tmkXzCd2Gqp93YGm .node circle,#mermaid-svg-tmkXzCd2Gqp93YGm .node ellipse,#mermaid-svg-tmkXzCd2Gqp93YGm .node polygon,#mermaid-svg-tmkXzCd2Gqp93YGm .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-tmkXzCd2Gqp93YGm .rough-node .label text,#mermaid-svg-tmkXzCd2Gqp93YGm .node .label text,#mermaid-svg-tmkXzCd2Gqp93YGm .image-shape .label,#mermaid-svg-tmkXzCd2Gqp93YGm .icon-shape .label{text-anchor:middle;}#mermaid-svg-tmkXzCd2Gqp93YGm .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-tmkXzCd2Gqp93YGm .rough-node .label,#mermaid-svg-tmkXzCd2Gqp93YGm .node .label,#mermaid-svg-tmkXzCd2Gqp93YGm .image-shape .label,#mermaid-svg-tmkXzCd2Gqp93YGm .icon-shape .label{text-align:center;}#mermaid-svg-tmkXzCd2Gqp93YGm .node.clickable{cursor:pointer;}#mermaid-svg-tmkXzCd2Gqp93YGm .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-tmkXzCd2Gqp93YGm .arrowheadPath{fill:#333333;}#mermaid-svg-tmkXzCd2Gqp93YGm .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-tmkXzCd2Gqp93YGm .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-tmkXzCd2Gqp93YGm .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-tmkXzCd2Gqp93YGm .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-tmkXzCd2Gqp93YGm .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-tmkXzCd2Gqp93YGm .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-tmkXzCd2Gqp93YGm .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-tmkXzCd2Gqp93YGm .cluster text{fill:#333;}#mermaid-svg-tmkXzCd2Gqp93YGm .cluster span{color:#333;}#mermaid-svg-tmkXzCd2Gqp93YGm div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-tmkXzCd2Gqp93YGm .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-tmkXzCd2Gqp93YGm rect.text{fill:none;stroke-width:0;}#mermaid-svg-tmkXzCd2Gqp93YGm .icon-shape,#mermaid-svg-tmkXzCd2Gqp93YGm .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-tmkXzCd2Gqp93YGm .icon-shape p,#mermaid-svg-tmkXzCd2Gqp93YGm .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-tmkXzCd2Gqp93YGm .icon-shape .label rect,#mermaid-svg-tmkXzCd2Gqp93YGm .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-tmkXzCd2Gqp93YGm .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-tmkXzCd2Gqp93YGm .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-tmkXzCd2Gqp93YGm :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    异步I/O

    操作系统主动通知

    数据拷贝由内核完成

    AIO/io_uring

    同步I/O

    应用程序主动等待

    数据拷贝由应用程序发起

    BIO/NIO/多路复用/信号驱动

    7.3 Java AIO示例

    // Java AIO 异步I/O
    public class AioServer {
    public static void main(String[] args) throws IOException {
    AsynchronousServerSocketChannel serverChannel =
    AsynchronousServerSocketChannel.open();
    serverChannel.bind(new InetSocketAddress(8080));

    serverChannel.accept(null, new CompletionHandler<>() {
    @Override
    public void completed(AsynchronousSocketChannel client, Object attachment) {
    // 继续接受下一个连接
    serverChannel.accept(null, this);

    ByteBuffer buffer = ByteBuffer.allocate(1024);
    // 异步读取,不阻塞
    client.read(buffer, buffer, new CompletionHandler<>() {
    @Override
    public void completed(Integer result, ByteBuffer attachment) {
    attachment.flip();
    // 数据已就绪,可直接使用
    System.out.println("收到数据: " +
    new String(attachment.array()));
    }

    @Override
    public void failed(Throwable exc, ByteBuffer attachment) {
    exc.printStackTrace();
    }
    });
    }

    @Override
    public void failed(Throwable exc, Object attachment) {
    exc.printStackTrace();
    }
    });

    // 主线程不阻塞,可以继续其他工作
    Thread.sleep(Integer.MAX_VALUE);
    }
    }


    8. 五大模型对比总结

    #mermaid-svg-gond0ltakCgHPz4V{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-gond0ltakCgHPz4V .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-gond0ltakCgHPz4V .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-gond0ltakCgHPz4V .error-icon{fill:#552222;}#mermaid-svg-gond0ltakCgHPz4V .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-gond0ltakCgHPz4V .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-gond0ltakCgHPz4V .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-gond0ltakCgHPz4V .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-gond0ltakCgHPz4V .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-gond0ltakCgHPz4V .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-gond0ltakCgHPz4V .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-gond0ltakCgHPz4V .marker{fill:#333333;stroke:#333333;}#mermaid-svg-gond0ltakCgHPz4V .marker.cross{stroke:#333333;}#mermaid-svg-gond0ltakCgHPz4V svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-gond0ltakCgHPz4V p{margin:0;}#mermaid-svg-gond0ltakCgHPz4V .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-gond0ltakCgHPz4V .cluster-label text{fill:#333;}#mermaid-svg-gond0ltakCgHPz4V .cluster-label span{color:#333;}#mermaid-svg-gond0ltakCgHPz4V .cluster-label span p{background-color:transparent;}#mermaid-svg-gond0ltakCgHPz4V .label text,#mermaid-svg-gond0ltakCgHPz4V span{fill:#333;color:#333;}#mermaid-svg-gond0ltakCgHPz4V .node rect,#mermaid-svg-gond0ltakCgHPz4V .node circle,#mermaid-svg-gond0ltakCgHPz4V .node ellipse,#mermaid-svg-gond0ltakCgHPz4V .node polygon,#mermaid-svg-gond0ltakCgHPz4V .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-gond0ltakCgHPz4V .rough-node .label text,#mermaid-svg-gond0ltakCgHPz4V .node .label text,#mermaid-svg-gond0ltakCgHPz4V .image-shape .label,#mermaid-svg-gond0ltakCgHPz4V .icon-shape .label{text-anchor:middle;}#mermaid-svg-gond0ltakCgHPz4V .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-gond0ltakCgHPz4V .rough-node .label,#mermaid-svg-gond0ltakCgHPz4V .node .label,#mermaid-svg-gond0ltakCgHPz4V .image-shape .label,#mermaid-svg-gond0ltakCgHPz4V .icon-shape .label{text-align:center;}#mermaid-svg-gond0ltakCgHPz4V .node.clickable{cursor:pointer;}#mermaid-svg-gond0ltakCgHPz4V .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-gond0ltakCgHPz4V .arrowheadPath{fill:#333333;}#mermaid-svg-gond0ltakCgHPz4V .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-gond0ltakCgHPz4V .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-gond0ltakCgHPz4V .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-gond0ltakCgHPz4V .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-gond0ltakCgHPz4V .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-gond0ltakCgHPz4V .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-gond0ltakCgHPz4V .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-gond0ltakCgHPz4V .cluster text{fill:#333;}#mermaid-svg-gond0ltakCgHPz4V .cluster span{color:#333;}#mermaid-svg-gond0ltakCgHPz4V div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-gond0ltakCgHPz4V .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-gond0ltakCgHPz4V rect.text{fill:none;stroke-width:0;}#mermaid-svg-gond0ltakCgHPz4V .icon-shape,#mermaid-svg-gond0ltakCgHPz4V .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-gond0ltakCgHPz4V .icon-shape p,#mermaid-svg-gond0ltakCgHPz4V .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-gond0ltakCgHPz4V .icon-shape .label rect,#mermaid-svg-gond0ltakCgHPz4V .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-gond0ltakCgHPz4V .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-gond0ltakCgHPz4V .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-gond0ltakCgHPz4V :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    模型5: AIO

    aio_read

    回调

    APP

    KERNEL

    模型4: 信号驱动

    sigaction

    SIGIO

    recvfrom

    APP

    KERNEL

    模型3: 多路复用

    select

    就绪列表

    recvfrom

    APP

    KERNEL

    模型2: NIO

    轮询

    就绪后拷贝

    APP

    KERNEL

    模型1: BIO

    recvfrom阻塞

    拷贝数据

    APP

    KERNEL

    内核空间

    内核

    用户空间

    应用程序

    总结表

    I/O模型等待数据阶段拷贝数据阶段系统调用次数适用场景
    阻塞I/O 阻塞 阻塞 1次 低并发,简单场景
    非阻塞I/O 不阻塞(轮询) 阻塞 N次+1次 CPU充足,连接数中等
    多路复用 阻塞(select) 阻塞 2次 高并发,如Nginx/Netty
    信号驱动 不阻塞 阻塞 2次 实际使用较少
    异步I/O 不阻塞 不阻塞 1次 高并发+高性能+高复杂度

    9. 实战选型:我该用哪个?

    #mermaid-svg-vUpcG0fHbzHHGKMe{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-vUpcG0fHbzHHGKMe .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-vUpcG0fHbzHHGKMe .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-vUpcG0fHbzHHGKMe .error-icon{fill:#552222;}#mermaid-svg-vUpcG0fHbzHHGKMe .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vUpcG0fHbzHHGKMe .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-vUpcG0fHbzHHGKMe .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vUpcG0fHbzHHGKMe .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vUpcG0fHbzHHGKMe .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-vUpcG0fHbzHHGKMe .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vUpcG0fHbzHHGKMe .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vUpcG0fHbzHHGKMe .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vUpcG0fHbzHHGKMe .marker.cross{stroke:#333333;}#mermaid-svg-vUpcG0fHbzHHGKMe svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vUpcG0fHbzHHGKMe p{margin:0;}#mermaid-svg-vUpcG0fHbzHHGKMe .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-vUpcG0fHbzHHGKMe .cluster-label text{fill:#333;}#mermaid-svg-vUpcG0fHbzHHGKMe .cluster-label span{color:#333;}#mermaid-svg-vUpcG0fHbzHHGKMe .cluster-label span p{background-color:transparent;}#mermaid-svg-vUpcG0fHbzHHGKMe .label text,#mermaid-svg-vUpcG0fHbzHHGKMe span{fill:#333;color:#333;}#mermaid-svg-vUpcG0fHbzHHGKMe .node rect,#mermaid-svg-vUpcG0fHbzHHGKMe .node circle,#mermaid-svg-vUpcG0fHbzHHGKMe .node ellipse,#mermaid-svg-vUpcG0fHbzHHGKMe .node polygon,#mermaid-svg-vUpcG0fHbzHHGKMe .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vUpcG0fHbzHHGKMe .rough-node .label text,#mermaid-svg-vUpcG0fHbzHHGKMe .node .label text,#mermaid-svg-vUpcG0fHbzHHGKMe .image-shape .label,#mermaid-svg-vUpcG0fHbzHHGKMe .icon-shape .label{text-anchor:middle;}#mermaid-svg-vUpcG0fHbzHHGKMe .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-vUpcG0fHbzHHGKMe .rough-node .label,#mermaid-svg-vUpcG0fHbzHHGKMe .node .label,#mermaid-svg-vUpcG0fHbzHHGKMe .image-shape .label,#mermaid-svg-vUpcG0fHbzHHGKMe .icon-shape .label{text-align:center;}#mermaid-svg-vUpcG0fHbzHHGKMe .node.clickable{cursor:pointer;}#mermaid-svg-vUpcG0fHbzHHGKMe .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-vUpcG0fHbzHHGKMe .arrowheadPath{fill:#333333;}#mermaid-svg-vUpcG0fHbzHHGKMe .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vUpcG0fHbzHHGKMe .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vUpcG0fHbzHHGKMe .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vUpcG0fHbzHHGKMe .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-vUpcG0fHbzHHGKMe .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vUpcG0fHbzHHGKMe .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-vUpcG0fHbzHHGKMe .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vUpcG0fHbzHHGKMe .cluster text{fill:#333;}#mermaid-svg-vUpcG0fHbzHHGKMe .cluster span{color:#333;}#mermaid-svg-vUpcG0fHbzHHGKMe div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vUpcG0fHbzHHGKMe .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-vUpcG0fHbzHHGKMe rect.text{fill:none;stroke-width:0;}#mermaid-svg-vUpcG0fHbzHHGKMe .icon-shape,#mermaid-svg-vUpcG0fHbzHHGKMe .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vUpcG0fHbzHHGKMe .icon-shape p,#mermaid-svg-vUpcG0fHbzHHGKMe .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-vUpcG0fHbzHHGKMe .icon-shape .label rect,#mermaid-svg-vUpcG0fHbzHHGKMe .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vUpcG0fHbzHHGKMe .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-vUpcG0fHbzHHGKMe .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-vUpcG0fHbzHHGKMe :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    简单Demo/内部工具连接数<100

    Web服务连接数1000~10000如Tomcat/Jetty

    超高并发海量长连接即时通讯/游戏

    CPU密集型数据处理

    你是什么场景?

    ✅ BIO开发效率最高

    ✅ I/O多路复用Netty/Redis/Nginx

    ✅ AIO或io_uringJava AIO慎用

    CPU密集型I/O模型影响小专注计算优化

    业界实践

    中间件/框架使用的I/O模型原因
    Nginx 多路复用(epoll) 高并发、可扩展性强
    Redis 多路复用(epoll) 单线程+事件驱动
    Netty 多路复用 Java生态最强NIO框架
    Tomcat 7+ 多路复用(NIO) 传统BIO → NIO演进
    Node.js 多路复用(libuv) 事件循环+异步
    Apache(Prefork) BIO 每请求一进程

    10. 进阶:io_uring——Linux的新一代异步I/O

    渲染错误: Mermaid 渲染失败: Lexical error on line 2. Unrecognized text. …R subgraph 传统AIO(POSIX) P1[接 ———————-^

    io_uring 工作流程:

    #mermaid-svg-bBlKbZVeWKRbIoEh{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-bBlKbZVeWKRbIoEh .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-bBlKbZVeWKRbIoEh .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-bBlKbZVeWKRbIoEh .error-icon{fill:#552222;}#mermaid-svg-bBlKbZVeWKRbIoEh .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-bBlKbZVeWKRbIoEh .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-bBlKbZVeWKRbIoEh .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-bBlKbZVeWKRbIoEh .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-bBlKbZVeWKRbIoEh .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-bBlKbZVeWKRbIoEh .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-bBlKbZVeWKRbIoEh .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-bBlKbZVeWKRbIoEh .marker{fill:#333333;stroke:#333333;}#mermaid-svg-bBlKbZVeWKRbIoEh .marker.cross{stroke:#333333;}#mermaid-svg-bBlKbZVeWKRbIoEh svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-bBlKbZVeWKRbIoEh p{margin:0;}#mermaid-svg-bBlKbZVeWKRbIoEh .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-bBlKbZVeWKRbIoEh .cluster-label text{fill:#333;}#mermaid-svg-bBlKbZVeWKRbIoEh .cluster-label span{color:#333;}#mermaid-svg-bBlKbZVeWKRbIoEh .cluster-label span p{background-color:transparent;}#mermaid-svg-bBlKbZVeWKRbIoEh .label text,#mermaid-svg-bBlKbZVeWKRbIoEh span{fill:#333;color:#333;}#mermaid-svg-bBlKbZVeWKRbIoEh .node rect,#mermaid-svg-bBlKbZVeWKRbIoEh .node circle,#mermaid-svg-bBlKbZVeWKRbIoEh .node ellipse,#mermaid-svg-bBlKbZVeWKRbIoEh .node polygon,#mermaid-svg-bBlKbZVeWKRbIoEh .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-bBlKbZVeWKRbIoEh .rough-node .label text,#mermaid-svg-bBlKbZVeWKRbIoEh .node .label text,#mermaid-svg-bBlKbZVeWKRbIoEh .image-shape .label,#mermaid-svg-bBlKbZVeWKRbIoEh .icon-shape .label{text-anchor:middle;}#mermaid-svg-bBlKbZVeWKRbIoEh .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-bBlKbZVeWKRbIoEh .rough-node .label,#mermaid-svg-bBlKbZVeWKRbIoEh .node .label,#mermaid-svg-bBlKbZVeWKRbIoEh .image-shape .label,#mermaid-svg-bBlKbZVeWKRbIoEh .icon-shape .label{text-align:center;}#mermaid-svg-bBlKbZVeWKRbIoEh .node.clickable{cursor:pointer;}#mermaid-svg-bBlKbZVeWKRbIoEh .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-bBlKbZVeWKRbIoEh .arrowheadPath{fill:#333333;}#mermaid-svg-bBlKbZVeWKRbIoEh .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-bBlKbZVeWKRbIoEh .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-bBlKbZVeWKRbIoEh .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bBlKbZVeWKRbIoEh .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-bBlKbZVeWKRbIoEh .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bBlKbZVeWKRbIoEh .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-bBlKbZVeWKRbIoEh .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-bBlKbZVeWKRbIoEh .cluster text{fill:#333;}#mermaid-svg-bBlKbZVeWKRbIoEh .cluster span{color:#333;}#mermaid-svg-bBlKbZVeWKRbIoEh div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-bBlKbZVeWKRbIoEh .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-bBlKbZVeWKRbIoEh rect.text{fill:none;stroke-width:0;}#mermaid-svg-bBlKbZVeWKRbIoEh .icon-shape,#mermaid-svg-bBlKbZVeWKRbIoEh .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bBlKbZVeWKRbIoEh .icon-shape p,#mermaid-svg-bBlKbZVeWKRbIoEh .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-bBlKbZVeWKRbIoEh .icon-shape .label rect,#mermaid-svg-bBlKbZVeWKRbIoEh .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bBlKbZVeWKRbIoEh .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-bBlKbZVeWKRbIoEh .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-bBlKbZVeWKRbIoEh :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    写入SQ

    内核消费

    写入CQ

    读取

    应用程序

    提交队列 Submission Queue

    内核

    完成队列 Completion Queue


    11. 面试高频追问

    Q1:Java的NIO是NIO(非阻塞)还是多路复用?

    答:Java NIO全称是New I/O,核心是多路复用模型,内部封装了select/poll/epoll,提供了非阻塞能力,本质是同步非阻塞 + 多路复用。

    Q2:为什么说Netty高性能?

    答:

  • 使用I/O多路复用(epoll)
  • 零拷贝技术
  • 无锁化设计(Pipeline)
  • 内存池(PooledByteBufAllocator)
  • Q3:select的1024限制是什么?

    答:select使用fd_set结构,默认最大文件描述符数量为FD_SETSIZE=1024,是编译时常量。epoll没有此限制。

    Q4:阻塞和非阻塞的区别是什么?

    • 阻塞:调用结果返回前,当前线程被挂起
    • 非阻塞:立即返回,不等待结果

    12. 总结脑图

    #mermaid-svg-sn5dtIA7nhMnZG57{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-sn5dtIA7nhMnZG57 .error-icon{fill:#552222;}#mermaid-svg-sn5dtIA7nhMnZG57 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-sn5dtIA7nhMnZG57 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-sn5dtIA7nhMnZG57 .marker.cross{stroke:#333333;}#mermaid-svg-sn5dtIA7nhMnZG57 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-sn5dtIA7nhMnZG57 p{margin:0;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge{stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .section–1 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section–1 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section–1 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section–1 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section–1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section–1 text{fill:#ffffff;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon–1{font-size:40px;color:#ffffff;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge–1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth–1{stroke-width:17;}#mermaid-svg-sn5dtIA7nhMnZG57 .section–1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-0 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-0 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-0 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-0 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-0 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-0{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-0{stroke-width:14;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-1 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-1 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-1 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-1 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-1 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-1{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-1{stroke-width:11;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-2 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-2 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-2 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-2 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-2 text{fill:#ffffff;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-2{stroke-width:8;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-3 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-3 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-3 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-3 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-3 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-3{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-3{stroke-width:5;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-4 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-4 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-4 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-4 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-4 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-4{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-4{stroke-width:2;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-5 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-5 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-5 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-5 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-5 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-5{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-5{stroke-width:-1;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-6 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-6 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-6 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-6 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-6 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-6{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-6{stroke-width:-4;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-7 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-7 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-7 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-7 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-7 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-7{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-7{stroke-width:-7;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-8 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-8 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-8 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-8 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-8 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-8{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-8{stroke-width:-10;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-9 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-9 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-9 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-9 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-9 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-9{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-9{stroke-width:-13;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-10 rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-10 path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-10 circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-10 polygon,#mermaid-svg-sn5dtIA7nhMnZG57 .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-10 text{fill:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .node-icon-10{font-size:40px;color:black;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .edge-depth-10{stroke-width:-16;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled circle,#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:lightgray;}#mermaid-svg-sn5dtIA7nhMnZG57 .disabled text{fill:#efefef;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-root rect,#mermaid-svg-sn5dtIA7nhMnZG57 .section-root path,#mermaid-svg-sn5dtIA7nhMnZG57 .section-root circle,#mermaid-svg-sn5dtIA7nhMnZG57 .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-sn5dtIA7nhMnZG57 .section-root text{fill:#ffffff;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-root span{color:#ffffff;}#mermaid-svg-sn5dtIA7nhMnZG57 .section-2 span{color:#ffffff;}#mermaid-svg-sn5dtIA7nhMnZG57 .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-sn5dtIA7nhMnZG57 .edge{fill:none;}#mermaid-svg-sn5dtIA7nhMnZG57 .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-sn5dtIA7nhMnZG57 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

    I/O模型

    阻塞I/O BIO

    同步阻塞

    简单但低并发

    每个连接一个线程

    非阻塞I/O

    同步非阻塞

    轮询机制

    CPU占用问题

    多路复用

    同步阻塞

    select/poll/epoll

    一个线程管多个连接

    Nginx/Netty/Redis

    信号驱动

    第一阶段异步

    第二阶段同步

    使用较少

    异步I/O AIO

    全程异步

    回调机制

    Windows IOCP

    Linux io_uring


    13. 一句话总结

    五种I/O模型的本质区别在于“数据就绪的等待阶段”和“数据拷贝阶段”是否阻塞:BIO全程阻塞,NIO和多路复用拷贝阶段阻塞,信号驱动等待阶段不阻塞,只有AIO全程非阻塞。高并发场景下,多路复用(epoll)是目前最成熟的主流方案,AIO(io_uring)是未来的方向。

    记忆口诀: BIO傻等数据来,NIO轮询问内核,多路复用管家看,信号驱动等通知,AIO搞定全自动。


    📌 如果你觉得这篇文章讲清楚了,欢迎点赞、收藏、评论交流!

    在这里插入图片描述

    🌺The End🌺点点关注,收藏不迷路🌺

    ⬆ ⬆ 顶部 ⬆ ⬆

    赞(0)
    未经允许不得转载:171主机测评 » 硬核图解:从BIO到AIO,五种I/O模型一网打尽!
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址