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

边栏推荐
- 第二批入围企业公示!年度TOP100智能网联供应商评选
- Flutter 如何使用在线转码工具将 JSON 转为 Model
- 零代码即可将数据可视化应用到企业管理中
- You are using pip version 21.1.2; however, version 22.1.2 is available
- How does flutter use the online transcoding tool to convert JSON to model
- 如何比较两个或多个分布:从可视化到统计检验的方法总结
- Detailed installation and use of performance test tool wrk
- Ideal L9, new trend of intelligent cockpit
- Reduce the pip to the specified version (upgrade the PIP through pycharm, and then reduce it to the original version)
- Huada 04A operating mode / low power consumption mode
猜你喜欢

L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验

The profound meaning of unlimited ecological development in Poka -- Multidimensional Interpretation of parallel chain

KT6368A蓝牙芯片的主从机之前透传功能说明,2.4G跳频自动连接

Resolving the conflict problem of the flutter Library

PyCharm 中出现Cannot find reference ‘imread‘ in ‘__init__.py‘

Development trend and path of SaaS industry in China

Multithreaded finalization

First order model realizes photo moving (with tool code) | machine learning

代理模式详解

树莓派初步使用
随机推荐
I really want to send a bunch of flowers
04A interrupt configuration
NIO、BIO、AIO
Mysql 通过表明获取字段以及注释
字符串习题总结2
权限想要细化到按钮,怎么做?
学习笔记23--多传感器信息融合基础理论(上)
Heartless sword Chinese English bilingual poem 003 The sea of books
leetcode:45. 跳跃游戏 II【经典贪心】
Collective search + drawing creation
系统测试主要步骤
first-order-model实现照片动起来(附工具代码) | 机器学习
L2 元年,Arbitrum Nitro 升级带来更兼容高效的开发体验
leetcode:55. 跳跃游戏【经典贪心】
Structure du disque
What aspects should we start with in the feasibility analysis of dry goods?
try-with-resources 中的一个坑,注意避让
为什么有的程序员能力一般却能拿到好offer?
985 test engineer is hanged. Who is more important in terms of education and experience?
降低pip到指定版本(通过cmp升级pip,在降低到原来版本)