当前位置:网站首页>The difference between IO and NiO
The difference between IO and NiO
2022-07-22 15:29:00 【JackieZhengChina】
1.IO and NIO The difference between
NIO Namely New IO stay JDK1.4 Introduction in .
IO and NIO Have the same function and purpose , But the implementation is different ,NIO The main use is block , therefore NIO Is more efficient than IO Much faster .
stay Java API Two sets of NIO, A set for standard input and output NIO, The other is network programming IO.
IO | NIO |
---|---|
Facing the flow | Facing buffer |
Blocking IO | Non blocking IO |
nothing | Selectors |
① Stream oriented and buffer oriented
Java IO It's flow oriented and Java NIO It's buffer oriented , Just as the focus of one is on the process , Another key point is that there is a stage .
stay Java IO Reading and writing data in is stream oriented (Stream) Of , Like a river . All the data keeps flowing forward , We can only touch the current water .
If you need to get the data before or after a data, you must cache the data yourself ( Pump water out of the river ), Not directly from the stream ( Because flow orientation means that we only have one aspect of data flow )
Java NIO The reading and writing of data in is buffer oriented (Buffer) Of , When reading, the whole block of data can be read into the buffer , When writing, the data in the whole buffer can be written together .
It's like building a dam on a river , Stream oriented data reading and writing only provides a data stream aspect , Buffer oriented IO Then we can see all the water ( Context of data ), In other words, it is very convenient to get the previous data or the latter data of a certain data in the buffer . This convenience comes at a price , Because we Buffers must be managed , This includes not allowing new data to overwrite useful data in the buffer that has not yet been processed ; Block the data in the buffer correctly , Distinguish what has been handled and what hasn't, etc .
② Blocking and non-blocking
Java IO It's blocked , If the data is not ready for a read-write data call , Or it can't be written at present , that Read and write operations will be blocked until the data is ready or the target is writable .
Java NIO It's non blocking , Every time the data Read and write calls will return immediately , And will now be readable ( Or writable ) The contents of are written to or output from the buffer , Even if no data is currently available , The call still returns immediately and does nothing to the buffer .
for instance :
IO and NIO Go shopping in the supermarket , If the supermarket does not have the required goods or the quantity is not enough , IO We will wait until the quantity of goods needed in the supermarket is enough, and then bring back all the goods we need .Java NIO Is different , No matter how many goods are needed in the supermarket , It will have the goods it needs , Buy it all immediately and return , Even goods that are not needed will be returned immediately .
IO Ask to complete the task at one time ,NIO Allow multiple tasks
2.IO and NIO The applicable scenarios of
NIO To make up for tradition IO Born of a lack of , however NIO It's also flawed , Should be NIO It's a buffer oriented operation , Every time data processing is done on the buffer , Then we must pay attention to : Before data processing, it is necessary to determine whether the data in the buffer is complete or has been read . without , Suppose the data only reads a part of it , So there's no point in incomplete data processing . Therefore, the buffer should be detected before each data processing .
Be careful : Every time you want to process data, you must ensure that the data has been prepared , But data processing can be done many times .
IO and NIO Each use scenario :
IO: A small amount of connection , These connections send a lot of data every time .
NIO: You need to manage thousands of connections that are open at the same time , These links only send a small amount of data at a time , For example, chat server
IO and NIO workflow
Java IO Workflow
because Java IO It's blocked , Therefore, when reading and writing to multiple streams, multiple threads are required . For example, on the Internet IO in ,Server The end uses a thread to listen on a port , Once a connection is accept, Create a new thread to handle the newly established connection .
among read/write It's blocked .
Java NIO Workflow
Java NIO Provide Selector Realize a single thread to manage multiple channel The function of .
among select The call may be blocked , It can also be non blocking . however read/write It's non blocking !
Learn from the article - Knowing and doing wandering
---------------------
author : Xiaofeifei
source :CSDN
original text :https://blog.csdn.net/weixin_42988712/article/details/109036394
Copyright notice : This is the author's original article , Please attach a link to the blog post !
Content analysis By:CSDN,CNBLOG One click reprint plugin for blog posts
边栏推荐
- NFC Introduction (2)
- seaborn barplot画图总结
- MySQL的增删查改(第二话)
- Two people line up to install locally & x360ce simulation handle Tutorial & xpadder handle simulation keyboard and mouse
- Redis高可用原理 主从哨兵集群
- static变量和全局变量的区别
- Summary of URL rules for real-time streaming of Hikvision, Dahua and yushila
- [cloud native kubernetes] jobs and cronjobs management under kubernetes cluster
- PLT draw and save the results
- OpenSSL self signed certificate issuance script -- the road to building a dream
猜你喜欢
随机推荐
Redis主从复制
Waiting insurance compliance 2022 series | what should you know about waiting insurance this year?
云原生Cloud Native
[cloud native kubernetes] jobs and cronjobs management under kubernetes cluster
Graffiti Wi Fi & ble SoC development slide strip (5) -- burning authorization
码蹄集 - MT3182 - 填矩阵
PLT draw and save the results
上传图片到本机iis服务器,结果以网页地址形式返回,可直接访问
K-means clustering modeling and programming
adb常见命令
res中values-swxxdp计算
【C语言-文件】数据终于可以出内存,到外面的世界看看了/(ㄒoㄒ)/~~
【07】函数调用:为什么会发生stack overflow?
Architecture design scheme (continuously updating ing)
LeetCode练习1——二叉树剪枝
涂鸦Wi-Fi&BLE SoC开发幻彩灯带(6)----幻彩灯带功能演示
TDengine實驗集群搭建 Success
商业智能BI分析思维:生产制造行业的现金周期(二)
vlc报错——“live555 error: no data received in 10s, aborting”
JDBC programming