当前位置:网站首页>BIO、NIO、AIO的区别和原理
BIO、NIO、AIO的区别和原理
2022-07-29 04:08:00 【Fanzongshen】
BIO、NIO、AIO的区别和原理
1.io模型
JAVA BIO:同步并阻塞,服务器实现模式为一个连接一个线程,即客户端有连接请求时服务器端就需要启动一个线程并处理,如果这个连接不做任何事情会造成不必要的开销,当然可以通过线程池机制改善
JAVA NIO:同步非阻塞,服务器实现模式为一个请求一个线程,即客户端发送的连接请求都会注册到多路复用器上,多路复用器轮询到连接有IO请求时才启动一个线程进行处理
JAVA AIO(NIO2):异步非阻塞,服务器实现模式为一个有效请求一个线程,客户端的I/O请求都是由OS先完成了再通知服务器应用去启动线程进行处理;
2.使用场景
BIO方式适用于连接数目比较小且固定的架构,这种方式对服务器资源要求比较高,并发局限于应用中,JDK1.4以前的唯一选择,但程序直观简单易理解。
NIO方式适用于连接数目多且连接比较短(轻操作)的架构,比如聊天服务器,并发局限于应用中,编程比较复杂,JDK1.4开始支持。
AIO方式使用于连接数目多且连接比较长(重操作)的架构,比如相册服务器,充分调用OS参与并发操作,编程比较复杂,JDK7开始支持。
3.BIO 同步并阻塞
tomcat采用的传统的BIO(同步阻塞IO模型)+线程池模式,对于十万甚至百万连接的时候,传统BIO模型是无能为力的:
①线程的创建和销毁成本很高,在linux中,线程本质就是一个进程,创建销毁都是重量级的系统函数
②线程本身占用较大的内存,像java的线程栈一般至少分配512K-1M的空间,如果系统线程过高,内存占用是个问题
③线程的切换成本高,操作系统发生线程切换的时候,需要保留线程的上下文,然后执行系统调用,如果线程数过高可能执行线程切换的时间甚至大于线程执行的时间,这时候带来的表现是系统load偏高,CPUsy使用率很高
④容易造成锯齿状的系统负载。系统负载是用活动线程数或CPU核心数,一旦线程数量高但外部网络环境不是很稳定,就很容易造成大量请求的结果同时返回,激活大量阻塞线程从而使系统负载压力过大。
4.NIO同步非阻塞
NIO基于Reactor,当socket有流可读或可写入socket,操作系统会相应的通知引用程序进行处理,应用再将流读取到缓冲区或写入操作系统。也就是,不是一个链接就要对应一个处理线程,而是一个有效请求对应一个线程,当连接没有数据时,是没有工作线程来处理的
nio只有acceptor的服务线程是堵塞进行的,其他读写线程是通过注册事件的方式,有读写事件激活时才调用线程资源区执行,不会一直堵塞等着读写操作,Reactor的瓶颈主要在于acceptor的执行,读写事件也是在这一块分发。
5.AIO异步非堵塞IO
AIO需要一个链接注册读写事件和回调方法,当进行读写操作时,只须直接调用API的read或write方法即可,这两种方法均为异步,对于读操作而言,当有流可读取时,操作系统会将可读的流传入read方法的缓冲区,并通知应用程序;对于写操作而言,当操作系统将write方法传递的流写入完毕时,操作系统主动通知应用程序
即,read/write方法都是异步的,完成后会主动调用回调函数;
边栏推荐
- 谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
- Extended operator of new features in ES6
- String template of ES6 new features and methods to simplify objects and functions
- 数据集成这个地方的过滤条件该咋写,用的啥语法?sql语法处理bizdate可以不
- Pointer constant and constant pointer
- Asp. Net MVC, how can the controller in the folder jump to the controller in the root directory?
- [principle] several ways of horizontal penetration
- The structure pointer must be initialized, and the pointer must also be initialized
- LCA board
- First knowledge of C language (3)
猜你喜欢

Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理

Install the laser of ROS_ scan_ Problems encountered in match library (I)

Ssl== certificate related concepts

Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation

Lvs+keepalived high availability deployment practical application

小程序:区域滚动、下拉刷新、上拉加载更多

VScode连接ssh遇到的问题

安装ros的laser_scan_matche库所遇到的问题(一)
![[deep learning CPU (part outside) - virtual memory]](/img/f7/4c72d583456f6f68c52424602ff5d9.png)
[deep learning CPU (part outside) - virtual memory]

SSL==证书相关概念
随机推荐
安装postgis时报找不到“POSTGIS_VERSION”这个函数
C language - character array - string array - '\0' -sizeof-strlen() -printf()
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
Why do I delete the original record (OP d) and then add a new one in Kafka when I update MySQL data
[BGP] small scale experiment
Fu Yingna: Yuan universe is the new generation of Internet!
MySQL Part 3
Codeforces round 810 (Div. 2) d. rain (segment tree difference)
Is there any way for Youxuan database to check the log volume that the primary cluster transmits to the standby cluster every day?
After I get the winfrom specific control ID from the database, I need to find the corresponding control through this ID and assign a value to the text text of the control. What should I do
Deconstruction assignment of new features of ES6
C language to achieve three chess game (detailed explanation)
Zhihuijun, a genius of Huawei, made a modular mechanical keyboard, which caused an earthquake in the geek circle. Netizens: This is the real customization
[untitled]
Codeforces Round #810 (Div. 2) D. Rain (线段树差分)
有一种密码学专用语言叫做ASN.1
[kvm] install KVM
Change the value of the argument by address through malloc and pointer
路西法98-生活记录ing
Opensql quick learning