当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
codeforces:E. Add Modulo 10【状态压缩 + 找规律】
技术人生 | 如何设定业务目标
redis summary_distributed cache
就刚刚,鸿蒙3.0发布了,华为还一口气发布了十一款产品
仿制药的未来商机--个人研发的体会
通信大学生走向岗位,哪些技能最实用?
来亲自手搭一个ResNet18网络
天翼云4.0来了!千城万池,无所不至!
详细教学——1688关键词搜索API操作流程
洛谷P4799 世界冰球锦标赛
“12306”的架构到底有多牛逼?
指针常量和常量指针概述
How to mitigate the attack of corporate account hijacking?
How a "cloud" can bring about new changes in the industry
一文看懂推荐系统:概要01:推荐系统的基本概念
Openharmony - 基于ArkUI(TS)开发颜色选择器
redis总结_多级缓存
2022最新彩虹表
golang刷leetcode滑动窗口(9) 颜色分类
Electronic Industry Inventory Management Pain Points and WMS Warehouse Management System Solutions