当前位置:网站首页>NIO、BIO、AIO
NIO、BIO、AIO
2022-06-24 19:34:00 【Nice2cu_Code】
IO模型
一、同步、异步
- 同步:线程自己去发起请求,获取结果(一个线程)
- 异步:线程自己发起结果,但不去获取结果,而是由其它线程获取结果后发送给这个线程(至少两个线程)
二、数据读取的过程
当用户线程调用 channel.read
或 stream.read
后,会切换至操作系统内核态来完成真正的数据读取,而读取又分为两个阶段,分别为:
- 等待数据阶段
- 复制数据阶段

三、同步阻塞、同步非阻塞、同步多路复用、异步非阻塞
阻塞 IO(同步)(BIO)
- 用户线程被阻塞,调用read方法后,在等待内核空间读取数据的时间内,不能进行其他操作,只能等待
非阻塞 IO(同步)
- 用户线程不会被阻塞,调用read方法后,即使内核空间没有读取到数据,会返回0,用户线程循环调用
read
方法,不会阻塞,直到复制阶段才被阻塞 - 数据的复制阶段仍然是阻塞的
- 用户线程不会被阻塞,调用read方法后,即使内核空间没有读取到数据,会返回0,用户线程循环调用
多路复用(同步,单个线程配合selector使用)(NIO)
- 阻塞直到事件发生
- 在等待阶段和复制阶段都会阻塞(单个channel的角度看,是阻塞的)
- 多个channel的角度看,是非阻塞的,一个channel中没有数据,不会阻塞,可以处理其他channel
异步非阻塞(异步 IO)(AIO)
- 用户调用read方法之后,内核空间立刻返回,告知得到用户的请求,等内核空间完成对应的操作之后,由另一个线程将结果返回
- 内核空间完成操作之后,会调用回调方法回传数据
- 由于立刻返回,所以异步一定不是阻塞,故异步一定是非阻塞的
阻塞 IO vs 多路复用(均为同步)
- 阻塞IO,处理一个连接的时候不能处理另一个连接,如果想要处理一个新的连接,必须等到上次的连接完全处理结束之后才可以处理新的连接
- 多路复用,一个 selector 可以同时处理多个 channel 的事件,某个 channel 中没有数据时,线程无需等待,可以处理其他事情
边栏推荐
- Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
- leetcode:55. 跳跃游戏【经典贪心】
- 波卡生态发展不设限的奥义——多维解读平行链
- Drag drag drag
- [200 opencv routines] 209 Color image segmentation in HSV color space
- L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
- 是真干不过00后,给我卷的崩溃,想离职了...
- DAO 中常见的投票治理方式
- 降低pip到指定版本(通过PyCharm升级pip,在降低到原来版本)
- Resolving the conflict problem of the flutter Library
猜你喜欢
Want to be a test leader, do you know these 6 skills?
Common voting governance in Dao
How to refine permissions to buttons?
Several classes of manual transactions
虚拟人的产业发展现状
排查到解决问题的过程:浏览器突然无法访问网页,错误代码:0x80004005,最终定位:“电脑打开热点,电脑就不能上网了”
Zero code can apply data visualization to enterprise management
Binary search tree template
Information update on automatic control principle
In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
随机推荐
Datakit agent realizes unified data aggregation in LAN
Double linked list implementation
The leader of ERP software in printing industry
What aspects should we start with in the feasibility analysis of dry goods?
Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
leetcode1720_ 2021-10-14
Graduation design of phase 6 of the construction practice camp
MySQL gets fields and comments by indicating
零代码即可将数据可视化应用到企业管理中
KT6368A蓝牙芯片的主从机之前透传功能说明,2.4G跳频自动连接
揭秘B站,程序员穿女装敲代码,效率更高是真的吗?
I really can't do it. After 00, I collapsed and wanted to leave
[untitled]
A girl has been making hardware for ten years. 。。
DAO 中常见的投票治理方式
St Table + two points
In the first year of L2, arbitrum nitro was upgraded to bring more compatible and efficient development experience
Implementation of heap sort and quick sort principle
排查到解决问题的过程:浏览器突然无法访问网页,错误代码:0x80004005,最终定位:“电脑打开热点,电脑就不能上网了”
Flutter 如何使用在线转码工具将 JSON 转为 Model