当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
无法超越的100米_百兆以太网传输距离_网线有哪几种?
技术人生 | 如何画业务大图
Interviewer: can you talk about optimistic locking and pessimistic locks
KunlunBase 1.0 is released!
千万级别的表分页查询非常慢,怎么办?
千万级QPS下服务如何才能平滑启动
LeetCode 2343. 裁剪数字后查询第 K 小的数字
Monitor is easy to Mars debut: distributed operations help TOP3000 across management gap
进程与线程
CUDA+Pycharm-gpu版本+Anaconda安装
LeetCode 1947. 最大兼容性评分和(状态枚举DP)
为何国内年轻人都抢购iPhone,因为它更实惠也更亲民
洛谷P1966 火柴排队
selenium安装和环境配置Firefox
Go编译原理系列6(类型检查)
golang源码分析(19)简单编译器-计算器
Remember the stuck analysis of an industrial automation control system in .NET
危及安全的常见物联网攻击有哪些?
如何确保智能工厂的安全?
CWE4.8:2022年危害最大的25种软件安全问题