当前位置:网站首页>NIO、BIO、AIO
NIO、BIO、AIO
2022-06-24 22:20: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 中沒有數據時,線程無需等待,可以處理其他事情

边栏推荐
- Cannot find reference 'imread' in 'appears in pycharm__ init__. py‘
- 学习笔记23--多传感器信息融合基础理论(上)
- St Table + two points
- Reduce the pip to the specified version (upgrade the PIP through CMP and reduce it to the original version)
- Learning notes 23-- basic theory of multi-sensor information fusion (Part I)
- Zero code can apply data visualization to enterprise management
- DAO 中常见的投票治理方式
- 如何比较两个或多个分布:从可视化到统计检验的方法总结
- NIO、BIO、AIO
- How does flutter use the online transcoding tool to convert JSON to model
猜你喜欢

Several schemes of traffic exposure in kubernetes cluster

“阿里健康”们的逻辑早就变了

Development trend and path of SaaS industry in China

Main steps of system test

Flutter 库冲突问题解决

Raspberry pie preliminary use

SAP interface debug setting external breakpoints

干货丨产品的可行性分析要从哪几个方面入手?

Interviewer: you said you are proficient in redis. Have you seen the persistent configuration?

华大04a工作模式/低功耗模式
随机推荐
产业互联网时代,并不存在传统意义上的互联网
Embedded development: tips and tricks -- clean jump from boot loader to application code
Structure du disque
Servlet details
Double linked list implementation
Object. Defineproperty and reflect Fault tolerance of defineproperty
NIO 零拷贝
Find the maximum value in each tree row [extension of one of the hierarchical traversals]
Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
零代码即可将数据可视化应用到企业管理中
降低pip到指定版本(通过cmp升级pip,在降低到原来版本)
Common voting governance in Dao
first-order-model实现照片动起来(附工具代码) | 机器学习
Ansible basic configuration
系统测试主要步骤
Summary of papers on traveling salesman problem (TSP)
linq查询集合类入门 案例武林高手类
Rotate the square array of two-dimensional array clockwise by 90 °
波卡生态发展不设限的奥义——多维解读平行链
02--- impossible phenomenon of longitudinal wave