当前位置:网站首页>Three components of NIO foundation
Three components of NIO foundation
2022-08-02 21:04:00 【I once more siege of the lion】
I. Channel
Channel is a bit similar to stream, it is a bidirectional channel for reading and writing data. It can read data from channel into buffer, or write buffer data into channel, and the previous stream orWhether it is input or output, channel is a lower level than stream.
Channels are typed, a string channel can only store string type data.
You can use Buffer as a container to take data out or add data to Channel.If Channel is a water tank, then Buffer is a scoop of water.
Common Channels are
- FileChannel
- DatagramChannel
- SocketChannel
- ServerSocketChannel
Second, Buffer
buffer is used to buffer read and write data. Use buffer to read or write data to channel. The buffer is the same as the array used to read and write files. Common buffers include
- ByteBuffer
- MappedByteBuffer
- DirectByteBuffer
- HeapByteBuffer
- ShortBuffer
- IntBuffer
- LongBuffer
- FloatBuffer
- DoubleBuffer
- CharBuffer
Three, Selector
Selector is generally called a selector, which can also be translated as a multiplexer. It is one of the core components of Java NIO and is used to check whether the state of one or more NIO Channels is in a readable and readable state.Writing
can implement a single thread to manage multiple channels, which avoids the overhead of thread context switching compared to using multiple threads.
The role of the selector is to cooperate with a thread to manage multiple channels and obtain the events that occur on these channels. These channels work in non-blocking mode and will not let threads hang on a channel.It is suitable for scenarios with a large number of connections but low traffic (low traffic)
Data reading occurs in the channel. In a single thread, the reading of multiple channels is blocked, and one channel is waiting for the client to read.During the process, other channels will be blocked. If multi-threading is used, it will be a challenge to system performance.
And the selector can also perform multiple operations in a single threadChannel is managed. By means of channel event monitoring, calling selector's select() will block until the channel has read and write ready events. When these events occur, the select method will return these events to thread for processing.
Selector maintains a set of registered Channels, and this registration relationship is encapsulated in SelectionKey.The selector can customize which events to listen to. The types of events that can be bound are:
The types of events that can be bound can be
- connect - fired when the client connects successfully
- accept - fires when the server side successfully accepts the connection
- read - Triggered when the data can be read, there are cases where the data cannot be read temporarily due to weak receiving ability
- write - Triggered when the data can be written out, there are cases where the data cannot be written temporarily due to the weak sending ability
边栏推荐
猜你喜欢

Win11dll文件缺失怎么修复?Win11系统dll文件丢失的解决方法

天翼云4.0分布式云赋能千行百业数字化转型

浅谈混迹力扣和codeforces上的几个月

灵动微电子发布低功耗 MM32L0130 系列 MCU 产品

千万级QPS下服务如何才能平滑启动

C#里如何简单的校验时间格式

How can services start smoothly under tens of millions of QPS

备战无人机配送:互联网派To C、技术派To B
Interviewer: can you talk about optimistic locking and pessimistic locks

技术人生 | 如何画业务大图
随机推荐
How to ensure the security of smart factories?
STL案例-招聘新员工
redis总结_分布式缓存
发挥云网融合优势,天翼云为政企铺设数字化转型跑道
【秒杀办法】根据二叉树的先序遍历、中序遍历、后序遍历快速创建二叉树
基于HDF的LED驱动程序开发(1)
selenium安装和环境配置Firefox
shell中awk命令的if条件语句引入外置变量
“12306”的架构到底有多牛逼?
「日志」深度学习 CUDA环境配置
进程与线程
mysql四种隔离级别
E-Surfing Cloud 4.0 Distributed Cloud Enables Digital Transformation of Thousands of Industries
电子行业库存管理痛点与WMS仓储管理系统解决方案
宝塔搭建实测-基于ThinkPHP5.1的wms进销存源码
NeRF: The Secret of 3D Reconstruction Technology in the Popular Scientific Research Circle
golang刷leetcode 经典(2)拓扑排序
如何构建准实时数仓?
千万级别的表分页查询非常慢,怎么办?
Smart Microelectronics Releases Low-Power MM32L0130 Series MCU Products