当前位置:网站首页>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
边栏推荐
- [深入研究4G/5G/6G专题-49]: 5G Link Adaption链路自适应-5-上行链路自适应ULLA-PUSCH信道
- Code Inspection for DevOps
- 记一次 .NET 某工控自动化控制系统 卡死分析
- POE交换机全方位解读(中)
- 洛谷P2345 MooFest G
- 就刚刚,鸿蒙3.0发布了,华为还一口气发布了十一款产品
- 仿制药的未来商机--个人研发的体会
- How to mitigate the attack of corporate account hijacking?
- 【秒杀办法】根据二叉树的先序遍历、中序遍历、后序遍历快速创建二叉树
- 为何国内年轻人都抢购iPhone,因为它更实惠也更亲民
猜你喜欢

arcgis 分子式标注

LeetCode 2336. 无限集中的最小数字(SortedSet)

固态硬盘接口类型介绍

宝塔搭建实测-基于ThinkPHP5.1的wms进销存源码

Taking advantage of cloud-network integration, e-Surfing Cloud has paved the way for digital transformation for government and enterprises

如何构建准实时数仓?

MySQL基本语法

Endanger the safety of common Internet attacks have?

TSF微服务治理实战系列(一)——治理蓝图

How a "cloud" can bring about new changes in the industry
随机推荐
2022最新彩虹表
TSF微服务治理实战系列(一)——治理蓝图
golang刷leetcode动态规划(9)不同路径 II
织梦自定义表单添加全选和全不选功能按钮
「日志」深度学习 CUDA环境配置
洛谷P1502 窗口的星星
洛谷P4316 绿豆蛙的归宿
golang刷leetcode 经典(6) 实现跳表
2022最新版SSM源码分析:一套教程助你深入理解底层原理,提高核心竞争力!
golang刷leetcode 经典(3) 设计推特
IDEA相关配置(特别完整)看完此篇就将所有的IDEA的相关配置都配置好了、设置鼠标滚轮修改字体大小、设置鼠标悬浮提示、设置主题、设置窗体及菜单的字体及字体大小、设置编辑区主题、通过插件更换主题
What skills are the most practical for college students in communications?
Five keys to a successful Industrial IoT deployment
基于HDF的LED驱动程序开发(1)
LeetCode 2333. 最小差值平方和(贪心)
洛谷P1966 火柴排队
天翼云4.0分布式云赋能千行百业数字化转型
How can services start smoothly under tens of millions of QPS
数据治理:数据集成和应用模式的演进
Technical life | How to draw a big picture of business