当前位置:网站首页>Preliminary understanding of NiO
Preliminary understanding of NiO
2022-07-27 04:20:00 【Knock the little crisp of the code】
Preface
The project simply uses Netty, Therefore, it is specially NIO Have some understanding . Find out NIO and Netty Huge space , It's not something you can get familiar with in a few days , So I plan to spend time studying hard later , Here, first record what you have learned , Yes NIO Some simple understanding of .
BIO
say NIO Before , First understand BIO.BIO It's blocking IO, It's stream oriented . For flow oriented understanding , It's from socket Read byte stream or write byte stream . How to understand blocking ? It is mainly manifested in two aspects :
① If a server is ready to start , Then the main thread has been waiting for the client to connect , During this wait, the main thread has been blocking .
② After the connection is established , Reading into socket Before information , The thread is also waiting , Always in a blocked state .
Understanding of the first point , That is, the server starts a service , Just block the thread and don't execute later , Until the first client connects to it , The code is executed later .
Understanding of the second point , That is to call accept() after , Just listen to the connection of a client , Only after the client sends the message , Will listen to the connection of the second client , If the first one accept The first client , And the client is slow to send messages , Then the thread keeps blocking , The second client cannot enter the server at this time accept Method , The second client cannot send messages , Only after the first client sends the message , Only the second client can send .
so ,BIO The blocking of is multiple socket When connecting to the same service ,socket There is mutual blockage between . In order to solve the blocking problem , Generally, thread pool is used to accept monitor , A client creates a thread , To avoid multiple socket Between them . But when there are many client connections , Thread pool scalability becomes poor . So it came out NIO.
NIO
Facing buffer
The so-called buffer , yes NIO Create your own buffer space , Applications read data or write data , Interact directly with the buffer space . The buffer space interacts with the underlying operating system . In the flow oriented BIO in , Byte streams interact directly with the operating system . therefore , stay NIO in , One more buffer , In this buffer , Data processing is more flexible , You can move the pointer back and forth to select data for processing .
Non blocking IO
Java NIO Non blocking mode of , Make a thread send a request to read data from a channel , But it can only get data that is currently available , If there is no data available , You will get nothing . Instead of keeping threads blocked , So until the data becomes readable , The thread can continue to do other things . So is nonblocking writing . A thread requests to write some data to a channel , But you don't have to wait for it to write completely , This thread can do other things at the same time .
Threads will usually be non blocking IO Free time for execution on other channels IO operation , So a single thread can now manage multiple input and output channels (channel)
Three core components
NIO There are three core components :Selector Selectors 、Channel The Conduit 、buffer buffer . Here is a brief understanding of the role of these three roles .NIO It uses Reactor Pattern , We will learn this mode in detail later .
Selector It's a selector , Used to register Channel and Channel Interested in IO event ( Connect 、 read 、 Writing, etc ).Selector It can be understood as a load balancing function ,IO After the event ,Selector Go to find the one registered here channel, Select corresponding channel To deal with the corresponding IO event .
Channel passageway , An interaction event between an application and the operating system is created 、 Channels for delivering content ( Note the connection to the operating system ). So since it's content delivery with the operating system , So the application can read data through the channel , You can also write data to the operating system through channels , And can read and write at the same time . therefore ,channel The essence is right socket There's a layer of packaging . stay BIO Direct use in socket Read and write data to the operating system , And in the NIO Use in channel Read and write data to the operating system .
buffer A buffer is a buffer oriented buffer , Used for and NIO Channels interact . Data is read into buffer from channel , Write... From buffer to channel . Buffer is essentially a block of data that can be written to , Memory from which data can then be read ( It's just an array ). This memory is packaged as NIO Buffer object , And provides a set of methods , For convenient access to the block memory .
Here, just talk about right NIO The understanding of the . Concrete API Use not for research , Later, it will be studied separately .
NIO understand
As can be seen from the above introduction ,NIO And BIO The difference between flow oriented and buffer oriented 、 The difference between blocking and non blocking .NIO It is a series of operations when the application program exchanges data with the underlying operating system . And the reading and writing events of the underlying operating system to the network are the same , It's not acceptable NIO Affected . therefore ,NIO It is an optimization made by reading and writing data between applications and operating systems . When the network is poor or the operating system level IO When there is a problem with the transmission ,NIO I can't help you .
Application scenarios
NIO Suitable for a large number of connections , A lot of IO Scenario of event . Non blocking improves performance . If there are not many clients connected in the business scenario ,IO There are not many events , Use BIO It can also meet the needs .
Last
Learn more, and then learn alone , Include AIO、Netty Wait for the network to transmit knowledge .
边栏推荐
- 标准C语言11
- First pass of routing strategy
- Big talk · book sharing | lean product development: principles, methods and Implementation
- "Gonna be right" digital collection is now on sale! Feel the spiritual resonance of artists
- Elastic开源社区:开发者招募
- Summer meal | rich people are different from what you think (day 5) + power system power flow simulation (documents and matlab code)
- js修改对象数组的key值
- Restful fast request 2022.2.2 release, supporting batch export of documents
- ArrayList与LinkedList区别
- JMeter学习笔记004-CSV文件行数控制循环次数
猜你喜欢

卷积神经网络——24位彩色图像的卷积的详细介绍

Nacos启动与登录

Lixia action | Yuanqi Digitalization: existing mode or open source innovation?

第二轮Okaleido Tiger即将登录Binance NFT,或持续创造销售神绩

452页13万字现代智慧乡镇雪亮工程整体解决方案2022版

Big talk · book sharing | lean product development: principles, methods and Implementation

ArrayList与LinkedList区别

spark练习案例(升级版)

Principle of bean validation --07

11. Zuul routing gateway
随机推荐
Golang sends email to the mail Library
list模拟实现
【比赛参考】PyTorch常用代码段以及操作合集
CloudCompare&PCL 匹配点中值(或标准差)距离抑制
ASP语音通知接口对接demo
Elastic开源社区:开发者招募
Manually build ABP framework from 0 -abp official complete solution and manually build simplified solution practice
记一次TCP丢包带来的重大性能问题
centos如何安装mysqldump
Search rotation sort array
Subject 3: Jinan Zhangqiu line 2
Stm32cubemx learning notes (41) -- eth interface +lwip protocol stack use (DHCP)
搜索旋转排序数组
Redis (IX) - redis distributed lock
leetcode每日一练:将句子排序
【SemiDrive源码分析】【驱动BringUp】41 - LCM 驱动 backlight 背光控制原理分析
Parallel desktop startup virtual machine "operation failed" problem solution
Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!
Towhee weekly model
Interview question 02.05. sum of linked list