当前位置:网站首页>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 .
边栏推荐
- Plato Farm全新玩法,套利ePLATO稳获超高收益
- influxDB 基础了解
- Which securities company has the lowest handling charge? Is it safe to open an account on your mobile phone
- centos如何安装mysqldump
- spicy之evt接口定义文件
- tcp协议知识详解
- Ribbon-负载均衡原理及部分源码
- Internet of things smart home project - Smart bedroom
- 【OBS】circlebuf
- 第二轮Okaleido Tiger即将登录Binance NFT,或持续创造销售神绩
猜你喜欢
![[Code] sword finger offer 04 search in two-dimensional array](/img/7d/a6693bfd24af9d9587539dda458d27.png)
[Code] sword finger offer 04 search in two-dimensional array

Delete the whole line of Excel, and delete the pictures together

Principle of bean validation --07

Introduction to JVM principle

"Gonna be right" digital collection is now on sale! Feel the spiritual resonance of artists

Five basic data structures of redis
![Abstract intelligent extraction [based on Bert technology]](/img/1c/7c1b0e9bc9af62308f4124104f6110.png)
Abstract intelligent extraction [based on Bert technology]

scala 不可变Map 、 可变Map 、Map转换为其他数据类型

Subject 3: Jinan Zhangqiu line 2

2022 operation of simulated examination question bank and simulated examination platform for safety production management personnel of hazardous chemical production units
随机推荐
Elastic认证考试:30天必过速通学习指南
物联网智能家居项目---智能卧室
webpack打包vue项目添加混淆方式,解决缓存问题
Daily question 1: delete continuous nodes with a total value of zero from the linked list
Redis (IX) - redis distributed lock
【无标题】
[untitled]
Golang sends email to the mail Library
3381. Mobile keyboard (Tsinghua University postgraduate entrance examination machine test question)
Big talk · book sharing | lean product development: principles, methods and Implementation
DINO 论文精度,并解析其模型结构 & DETR 的变体
288页18万字智能化校园总体设计 目录
Collating strings
【MySQL系列】MySQL索引事务
A. Round Down the Price
js实现页面跳转与参数获取加载
452 pages, 130000 words, the overall solution of modern smart Township Xueliang project 2022 Edition
Eureka-服务注册中心
【比赛参考】PyTorch常用代码段以及操作合集
ffmpeg合并视频功能