当前位置:网站首页>Differences and principles of bio, NiO and AIO
Differences and principles of bio, NiO and AIO
2022-07-29 04:12:00 【Fanzongshen】
BIO、NIO、AIO The differences and principles of
1.io Model
JAVA BIO: Synchronize and block , The server implements a pattern of one thread per connection , That is, when the client has a connection request, the server needs to start a thread and process , If this connection doesn't do anything, it will cause unnecessary overhead , Of course, it can be improved through the thread pool mechanism
JAVA NIO: Synchronous nonblocking , The server implementation mode is one request, one thread , That is, the connection requests sent by the client will be registered on the multiplexer , The multiplexer polls the connection to have IO Start a thread to process when requested
JAVA AIO(NIO2): Asynchronous non-blocking , The server implementation mode is a valid request for a thread , Client's I/O All requests were made by OS First complete and then inform the server application to start the thread for processing ;
2. Use scenarios
BIO It is suitable for architectures with a small number of connections and a fixed number of connections , This approach is more demanding on server resources , Concurrency is limited to the application ,JDK1.4 The only choice before , But the program is straightforward and easy to understand .
NIO The method is suitable for a large number of short connections ( Light operation ) The architecture of , For example, chat servers , Concurrency is limited to the application , Programming is complicated ,JDK1.4 Start supporting .
AIO Mode is used for many and long connections ( Re operation ) The architecture of , Like the album server , Call full OS Participate in concurrent operations , Programming is complicated ,JDK7 Start supporting .
3.BIO Synchronize and block
tomcat The traditional BIO( Synchronous blocking IO Model )+ Thread pool mode , For 100000 or even millions of connections , Tradition BIO The model is powerless :
① Threads are expensive to create and destroy , stay linux in , A thread is essentially a process , Creation and destruction are heavyweight system functions
② The thread itself occupies a large amount of memory , image java The thread stack of is generally allocated at least 512K-1M Space , If the system thread is too high , Memory usage is a problem
③ High switching cost of threads , When the operating system switches threads , The context of the thread needs to be preserved , Then make a system call , If the number of threads is too high, the time of thread switching may be even greater than the time of thread execution , The performance brought by this time is the system load High ,CPUsy The usage rate is very high
④ It's easy to create jagged system load . The system load is expressed in the number of active threads or CPU The core number , Once the number of threads is high but the external network environment is not very stable , It's easy to cause a large number of requests to return at the same time , Activate a large number of blocking threads, so that the system load pressure is too high .
4.NIO Synchronous nonblocking
NIO be based on Reactor, When socket Stream readable or writable socket, The operating system will notify the reference program accordingly to handle , The application then reads the stream to the buffer or writes it to the operating system . That is to say , Not a link should correspond to a processing thread , Instead, a valid request corresponds to a thread , When the connection has no data , There is no worker thread to handle it
nio Only acceptor The service thread of is blocked , Other read / write threads register events , The thread resource area is called to execute only when a read-write event is activated , It won't be blocked all the time waiting for reading and writing ,Reactor The bottleneck lies in acceptor Implementation , Read and write events are also distributed here .
5.AIO Asynchronous non blocking IO
AIO You need a link to register read / write events and callback methods , When reading and writing , Just call API Of read or write The method can , Both methods are asynchronous , For read operations , When there is a stream to read , The operating system passes a readable stream into read Method , And notify the application ; For write operations , When the operating system will write When the stream passed by method is finished writing , The operating system actively notifies applications
namely ,read/write Methods are asynchronous , After completion, the callback function will be actively called ;
边栏推荐
- Routing knowledge
- MPU6050
- When defining an array, the size must be constant
- LDP --- 标签分发协议
- VScode连接ssh遇到的问题
- Data mining -- code implementation of association analysis example (Part 2)
- flink-sql 如何设置 sql执行超时时间
- Don't the JDBC SQL connector of the big guys Flink now support all databases, such as vertica?
- [Openstack] keystone,nova
- Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
猜你喜欢
mmdetection初步使用
Deep understanding of browser caching mechanism (HTTP)
BGP的基础配置---建立对等体、路由宣告
Why are there so many unknowns when opengauss starts?
Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理
Fu Yingna: Yuan universe is the new generation of Internet!
C语言实现三子棋游戏(详解)
小程序:区域滚动、下拉刷新、上拉加载更多
Object array merges elements according to a field
First knowledge of C language (3)
随机推荐
Problems encountered in vscode connection SSH
SQL server how to judge when the parameter received by the stored procedure is of type int?
SQL window function
Nacos registry
SQL语句 关于字段转换怎么写
Communication between parent-child components and parent-child components provide and inject
MPU6050
Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
How to write SQL statements about field conversion
[deep learning CPU (part outside) - virtual memory]
MySQL Part 3
GBase 8a特殊场景下屏蔽 ODBC 负载均衡方式?
Change the value of the argument by address through malloc and pointer
店铺排名问题,如何解决?
Codeforces round 810 (Div. 2) d. rain (segment tree difference)
I. creation and constraint of MySQL table
Design of environment detection system based on STM32 and Alibaba cloud
SVG--loading动画
基于STM32和阿里云的环境检测系统设计