I like to use simple language to explain knowledge points
Long-term sharing of original Java articles, advanced architect learning notes and learning materials
Like you can like attention, learn together, progress together
0) Haha, ACTUALLY I am a clicker,NIO is not awesome IO, non-blocking IO
NIO is an idea, a non-blocking IO communication idea, and Netty is a NIO framework based on NIO.
If you want to take a minute to figure out what Netty is, let’s just say that Netty is a simple Jar package that is used as a communication component. Okay
What is non-blocking? (Why didn’t I say what IO is? Since you have learned NIO, I can’t help it if you don’t know what IO is.)
This article is also a brief introduction to NIO, want to see all kinds of source code students can bypass – –
1) Asynchronous non-blocking examples:
Old Zhang love tea, nonsense do not say, boiled water.
Characters: Lao Zhang, two kettles (ordinary kettles, referred to as kettles; A ringing kettle for short).
Lao Zhang puts the kettle on the fire and waits for the water to boil. (Synchronous blocking)
———-> Lao Zhang feels a bit silly
Lao Zhang put the kettle on the fire and went to the living room to see if MAO Cheater was boiling. (Synchronous non-blocking)
———-> Lao Zhang feels a bit silly
So I went high-end and bought one of those whistles. After the water boils, it can beep ~~~~ loudly.
Lao Zhang put the kettle on the fire and waited for the water to boil. (Asynchronous blocking)
———> Lao Zhang feels a bit silly
Lao Zhang put the kettle on the fire and went to the living room to see MAO Cheat. He didn’t look at the kettle until it rang and then went to get it. (Asynchronous non-blocking)
———-> Well, Lao Zhang thinks he’s awesome
2) Summary: In a nutshell, the non-blocking mode of Java NIO is to have a thread send a request from a channel to read (or write) data (such as boiling water).
It does not keep the thread blocked, so it can continue to do other things before reading (or writing) data. (E.g., living room hair con)
3) Comparison of IO VS NIO (differences)
1.IO can only implement blocking network communication. NIO enables non-blocking network communication.
2. Standard IO operates based on byte/character streams. NIO operates on a Channel basis. (In other words, the gateway to a woman’s heart is XXX…)
3. The read and write of a stream is usually unidirectional, either input or output, not both input and output. A channel is bidirectional. Data can be written to or read from a channel.
4) Learning objectives: Although we will not write NIO directly to complete our network layer communication, we will use a mature NETWORK framework based on NIO to achieve our network layer. E.g. Netty and Mina. However, understanding the NIO network programming process helps us to have a deeper understanding of netty, MINA and other network frameworks so that we can use them better. Somebody asked me, how does not learning this affect my typing, so to speak, not at all.
5) Now that you know what NIO is, let’s look at the three important components of Java NIO:
A Channel, a Buffer, a Selector.
Of course, analog learning is a better learning method, here I still do comparison with the traditional IO, I hope he will not hit me
6) A Channel, as its name implies, is a way to something, providing a Channel for something.
1. In traditional IO, streams are unidirectional. For example, InputStream can only read, and OutputStream can only write.
A Channel is bidirectional and can be used for both read and write operations.
2. The specific implementation of common channel FileChannel, SocketChanel, ServerSocketChannel, DatagramChannel, etc
With specific implementation flow FileInputStream, FileOutputStream, FileReader, FileWriter, node stream flow and so on function similar to the packaging stream buffer
7) A Buffer, one of the most important things in NIO, is actually a container, which is a continuous array. All data is read and written in NIO without Buffer. In NIO, read data can only be placed in Buffer. Similarly, data is written to Buffer first.
The figure above depicts the process of sending data from a client to a server, which then receives the data.
In simple terms, to use a Channel to pass data, you must first throw the data into a Buffer.
In NIO, Buffer is a top-level superclass, which is an abstract class. Common subclasses of Buffer include:
ByteBuffer, IntBuffer CharBuffer, LongBuffer DoubleBuffer, FloatBuffer, ShortBuffer, etc
8) Selector, it can be said that it is the most critical part of NIO. The role of Selector is to poll each registered Channel. Once a registered event is found in a Channel, it will get the event and process it.
In traditional socket programming, the Accept method blocked until a client request was received and returned to the socket for processing. The whole process is like the example above, until the kettle is boiling (the response goes back) before the next request can be processed. Of course we can also use the thread pool model.
NIO provides a better solution. The Selector Selector can detect whether an event has occurred on multiple registered channels, and if so, fetch the event and then respond to each event accordingly. In this way, you can manage multiple channels, and thus multiple connections, with just one thread. This allows functions to be called only when a connection actually has an read or write event, greatly reducing system overhead, eliminating the need to create a thread for each connection, maintaining multiple threads, and avoiding the overhead of context switching between multiple threads. And it is processed sequentially, based on channels and buffers to transfer and save data.
One of the key classes associated with Selector is SelectionKey. A SelectionKey represents an incoming event. These two classes form the key logic for the server to process the business.