JAVA IO package and NIO

Table of contents

JAVA NIO

NIO buffer

NIO non-blocking

Channel

Buffer

Selector


 

JAVA NIO

NIO mainly has three core parts: Channel, Buffer, and Selector. Traditional IO is based on byte streams and words
Operates on stream, while NIO operates based on Channel and Buffer (buffer). Data is always read from the channel to the buffer.
in, or written from the buffer to the channel . Selector (selection area) is used to monitor events of multiple channels (for example: connection opening,
data arrives). Therefore, a single thread can listen to multiple data channels.
The first biggest difference between NIO and traditional IO is that IO is stream-oriented and NIO is buffer-oriented.

 

NIO buffer

Java IO is stream-oriented meaning that one or more bytes are read from the stream at a time, and until all bytes have been read, they are not cached in any
place. Additionally, it cannot move data in the stream forward or backward. If you need to move the data read from the stream back and forth, you need to buffer it first
Save to a buffer. NIO's buffer-oriented approach is different. The data is read into a buffer that it processes later, if needed.
Move forward and backward in the buffer. This increases flexibility in processing. However, you also need to check whether the buffer contains all
There is data you need to process. Also, make sure that when more data is read into the buffer, unprocessed data in the buffer is not overwritten.
data.
 

NIO non-blocking

Various IO streams are blocked. This means that when a thread calls read() or write(), the thread is blocked until there is
Some data is read, or data is completely written. The thread cannot do anything else during this period. NIO's non-blocking mode,
Causes a thread to send a request to read data from a channel, but it can only get the currently available data. If there is currently no data available,
When you use it, you get nothing. Instead of keeping the thread blocked, so until the data becomes readable, the thread can
Move on to other things. The same goes for non-blocking writes. A thread requests to write some data to a channel, but does not need to wait for it
Completely written, this thread can do other things at the same time. Threads usually use the idle time of non-blocking IO to perform other tasks on other channels.
Perform IO operations, so a single thread can now manage multiple input and output channels.

Channel

First let’s talk about Channel, which is mostly translated as “channel” in China. Channel and Stream in IO are almost the same
hierarchical. It’s just that Stream is one-way , such as: InputStream, OutputStream, and Channel is two-way
, can be used for both reading and writing operations.
The main implementations of Channel in NIO are:
1. FileChannel
2. DatagramChannel
3. SocketChannel
4. ServerSocketChannel
You can guess the reason by looking at the names here: they can correspond to file IO, UDP and TCP (Server and Client) respectively.
The case demonstrated below basically revolves around these four types of Channels.

Buffer

Buffer, hence the name, is actually a container and a continuous array . Channel provides access from files,
The network is a channel for reading data, but the data read or written must go through the Buffer.

The above figure describes the process of sending data from a client to the server, and then the server receives the data. Client sends
When reading data, the data must be stored in the Buffer first, and then the content in the Buffer is written to the channel. The server must receive data here
The data must be read into the Buffer through the Channel, and then the data must be taken out of the Buffer for processing.
In NIO, Buffer is a top-level parent class, which is an abstract class. Commonly used subclasses of Buffer are:
ByteBuffer、IntBuffer、 CharBuffer、 LongBuffer、 DoubleBuffer、FloatBuffer、
ShortBuffer

Selector

The Selector class is the core class of NIO. The Selector can detect whether events occur on multiple registered channels. If something happens
When an event occurs, the event is obtained and corresponding response processing is performed for each event . In this way, just using a single thread can
To manage multiple channels, that is, manage multiple connections. This will only be called when there are actually read and write events on the connection.
Functions are used to read and write, which greatly reduces system overhead, and there is no need to create a thread for each connection and do not need to maintain it.
Multiple threads, and avoids the overhead caused by context switching between multiple threads.

 

Guess you like

Origin blog.csdn.net/weixin_38340874/article/details/122087795