当前位置:网站首页>Differences among bio, NiO and AIO
Differences among bio, NiO and AIO
2022-07-27 05:23:00 【A dream without trace BZY】
One 、BIO:Blocking IO, Blocking io, Efficiency is very low , Because it is often blocked
1、 Working principle diagram

2、java Code
2.1、 Server code :

Server creation ServerSocket object , Bind the address and port . Enter the loop and wait to be linked ( If there is no link, it will be blocked all the time ss.accept() this ), Once a client is connected , adopt ss.accept() Method , Restart the thread to execute ( The reason why the thread is restarted is for the sake of high concurrency )
2.2、 Client code

3、BIO Cause of obstruction
- accept It's the blocking method , No client has been blocked
- read It's the blocking method , The client is just connected , But I didn't write anything , The server side is blocked in read Method this
- write It's the blocking method , If the server doesn't write anything, it will be blocked here
4、 Use scenarios :
When there are few clients, you can use , Because it's easy to write
Two 、NIO:
1、New Non-Blocking: Non blocking IO( Single threaded mode )
1.1、 Working principle diagram

There is a in the main thread of the server selector( Selectors ) There are two things to do : One thing is to check whether there are new clients to connect every once in a while , If yes, connect the client to the server . The second is to check whether there are read and write operations between the client and server connected to the server , If yes, handle . That is, one thread does everything
1.2、java Code

1、ssc.configureBlocking(false): Set to non blocking
2、ssc.register(): Registration selector to do , What this place does is to see if there is a client to connect
3、selector.select(): It's the blocking method , If there is a client to connect, go down
4、 Server side ServerSocket It can be seen as a panel with many sockets .keys amount to selector A collection of listeners registered on each socket of the server , When a client wants to connect up , Just put this connection event into keys Inside , Then cycle keys, And you must remove( If you don't remove Then we will deal with this event again next time ).handle It is the specific processing process


1、key.isAcceptable==true It means that a client wants to connect , Set to false The purpose is to cancel blocking
2、sc.register(): The thing to do in this place is to check whether the connected client has write operations ( Compared with the server, it is read )
3、key.isReadable==true Note that the connected client has write operations
1.3、 Use scenarios
Because always selector Single thread execution , As long as there is a place blocked , The whole program will not run , Plus ByteBuffer complex , So few people use nio Single threaded mode
ByteBuffer It's a particularly complicated thing , So later netty emerge as the times require
2、NIO-reactor(NIO Responsive programming mode )
2.1、 Working principle diagram

selector Do two things : First, check whether there are new clients to connect every once in a while , If yes, connect the client to the server . Second, when the connected client wants to read and write , Give these read and write operations to the thread pool ( In this way, it is no longer a single thread )
3、 ... and 、AIO: asynchronous IO
Working principle diagram

The operating system notifies the callback function when there is a client to connect , Then the callback function handles ; When there are read and write operations , Continue to call the callback functions related to reading and writing . But there is still ByteBuffer, So it's not often used
Four 、 Three more
1、BIO( Synchronous blocking IO):
Whether the connection or read-write operation is not completed, it will block ( Connect 、 read 、 Writing methods are blocking methods ). Because it is often blocked , So it's inefficient , Less commonly used ; But the writing is simple , If the number of users is small, you can consider using
2、NIO( Synchronous nonblocking IO):
All are selector Poll every once in a while to see if there are any connection operations or read-write operations to be processed ( Single thread selector Handle read and write operations by yourself 、 Multithreading selector Send read and write operations to the thread pool ). however ByteBuffer metamorphosis , So it's not often used 
Every time selector After connecting a request, a channel Correspond to the client one by one , The read and write operations between the client and the server are channel In the middle of , utilize byetbuffer You can manipulate interactive data and store data , Don't like stream It disappeared immediately because the interaction was over
3、AIO( Asynchronous non-blocking IO):
When there is a client to connect, the operating system notifies the callback function , Then the callback function handles ; When there are read and write operations , Continue to call the callback functions related to reading and writing . But there is still ByteBuffer, So it's not often used .
The above summary is :BIO clutch in the hand ,NIO polling ,AIO It's automatic . But not often
NIO、AIO linux Both the upper and lower layers of the system are epoll,epoll Itself is polling , It's just AIO Encapsulate it . So in linux On the system ,NIO Efficient than AIO Efficient , This is also netty Yes NIO The reason for encapsulation
边栏推荐
猜你喜欢

Constraints of MySQL table

JVM Part 1: memory and garbage collection -- runtime data area 4 - program counter

torch中乘法整理,*&torch.mul()&torch.mv()&torch.mm()&torch.dot()&@&torch.mutmal()

Acticiti中startProcessInstanceByKey方法在variable表中的如何存储

Derivation and explanation of PBR physical illumination calculation formula

JVM上篇:内存与垃圾回收篇九--运行时数据区-对象的实例化,内存布局与访问定位

MQ message queue is used to design the high concurrency of the order placing process, the generation scenarios and solutions of message squeeze, message loss and message repetition

Could not autowire. No beans of ‘userMapper‘ type found.

35.滚动 scroll

Introduction to dynamic memory functions (malloc free calloc realloc)
随机推荐
文件处理(IO)
Summary of knowledge points (I)
[CSAPP] Application of bit vectors | encoding and byte ordering
2、 MySQL advanced
Could not autowire. No beans of ‘userMapper‘ type found.
Solution to Dlib installation failure
简化JDBC的MyBits框架
JVM Part 1: memory and garbage collection part 11 -- execution engine
JVM上篇:内存与垃圾回收篇六--运行时数据区-本地方法&本地方法栈
Three waiting methods of selenium and three processing methods of alert pop-up
Introduction to dynamic memory functions (malloc free calloc realloc)
JVM上篇:内存与垃圾回收篇二--类加载子系统
How does the TCP server handle multiple client connections on one port (one-to-one or one to many)
redis锁
Quoted popular explanation
JVM part I: memory and garbage collection part II -- class loading subsystem
34. 分析flexible.js
B1028 census
接收方设置并发量和限流
秒杀系统设计