当前位置:网站首页>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 中沒有數據時,線程無需等待,可以處理其他事情
边栏推荐
- 代理模式详解
- 03--- antireflective film
- 01--- conditions for interference of two trains of waves at the meeting place
- Why can some programmers get good offers with average ability?
- Machine learning: gradient descent method
- Binary search tree template
- Description of software version selection of kt6368a Bluetooth dual-mode transparent chip
- Development of live broadcast software app, and automatic left-right sliding rotation chart advertising
- Réduire le PIP à la version spécifiée (mettre à jour le PIP avec pycharm avant de le réduire à la version originale)
- ansible基本配置
猜你喜欢
Heartless sword Chinese English bilingual poem 003 The sea of books
一个女孩子居然做了十年硬件。。。
权限想要细化到按钮,怎么做?
NIO、BIO、AIO
Main steps of system test
Information update on automatic control principle
YGG 近期游戏合作伙伴一览
好想送对象一束花呀
CV2 package guide times could not find a version that satisfies the requirement CV2 (from versions: none)
想当测试Leader,这6项技能你会吗?
随机推荐
MySQL gets fields and comments by indicating
Detailed explanation of agency mode
为什么有的程序员能力一般却能拿到好offer?
解决dataframe报错ValueError: Cannot take a larger sample than population when ‘replace=False‘
磁盤的結構
First order model realizes photo moving (with tool code) | machine learning
权限想要细化到按钮,怎么做?
TCP RTT测量妙计
Industrial development status of virtual human
Servlet详解
03--- antireflective film
代理模式详解
985 test engineer is hanged. Who is more important in terms of education and experience?
Cannot find reference 'imread' in 'appears in pycharm__ init__. py‘
Binary search tree template
985测试工程师被吊打,学历和经验到底谁更重要?
A girl has been making hardware for ten years. 。。
try-with-resources 中的一个坑,注意避让
Learning notes 23-- basic theory of multi-sensor information fusion (Part I)
LINQ query collection class introductory cases Wulin expert class