I reviewed the concepts related to Io, NIO and AIO in the morning and recorded some of these points.
from the programming language level
BIO | NIO | AIO in the Java perspective, understanding, Linux C also has the concept of AIO (library), these concepts do not know what reason is fired up, here only from the Java perspective.
- BIO, synchronous blocking Io, simple comprehension: One connection to one thread
- NIO, synchronous non-blocking IO, simple comprehension: one request a thread
- AIO, asynchronous non-blocking IO, simple comprehension: A valid request for a thread
BIO
Before JDK1.4, writing a network request in Java is to create a serversocket, and then, when the client establishes the socket, it asks if the thread can handle it, or if not, either waits or is rejected. That is: A connection that requires the server to correspond to a processing thread.
Nio
In Java, a set of APIs is provided in JDK1.4 and later versions to specifically manipulate non-blocking I/O, and we can find related classes and interfaces in the Java.nio package and its child packages. Since this API is the new I/O API provided by the JDK, it is also called New I/O, which is the origin of package name NiO. This set of APIs consists of three main components: buffer (buffers), channel (Channels), and non-blocking I/O core classes. In understanding NIO, it is necessary to differentiate between New I/O or non-blocking io,new I/O is a Java package, and NiO is a non-blocking IO concept. This is about the latter one.
NiO itself is based on event-driven thinking, its main solution is the big concurrency problem of bio: In network applications using synchronous I/O, if you want to process multiple client requests at the same time, or if the client wants to communicate with more than one server at the same time, it must be processed using multithreading. That is, each client request is assigned to a thread that is processed separately. While this can meet our requirements, it will also lead to another problem. Since each thread is created, it is necessary to allocate a certain amount of memory (also known as a working memory) to the thread, and the operating system itself has a certain limit on the total number of threads. If the client has too many requests, the server may be overwhelmed to deny requests from the client, or even the servers may be paralyzed.
NIO is based on reactor, when the socket has a stream-readable or writable socket, the operating system notifies the reference program to process it, and the application then reads the stream into the buffer or writes to the operating system. In other words, this time, is not a connection to a processing thread, but a valid request, corresponding to a thread, when the connection has no data, there is no working thread to handle.
Aio
Unlike NiO, when it comes to read and write operations, only the read or write method of the API can be called directly. Both methods are asynchronous, and for a read operation, when a stream is readable, the operating system streams the readable buffer into the Read method and notifies the application that, for a write operation, the operating system proactively notifies the application when it finishes writing the stream passed by the write method. It can be understood that the Read/write method is asynchronous and will invoke the callback function after completion. In JDK1.7, this part of the content is called nio.2, with the following four asynchronous channels added mainly under the Java.nio.channels package:
- Asynchronoussocketchannel
- Asynchronousserversocketchannel
- Asynchronousfilechannel
- Asynchronousdatagramchannel
The Read/write method, which returns an object with a callback function, calls the callback function directly after the read/write operation is performed.
Implementation Principle
Say the principle of implementation, but also from the operating system IO model to understand
According to the UNIX network programming, the IO model can be divided into: blocking IO, non-blocking IO, IO multiplexing, signal driven IO and asynchronous Io, divided into two categories according to POSIX standard: synchronous IO and asynchronous IO. How do you differentiate it? First IO operation is actually divided into two steps: Initiate IO request and actual IO operation, the difference between synchronous IO and asynchronous IO is whether the second step is blocked, if the actual IO read/write blocking request process, then is synchronous io, so blocking IO, non-blocking IO, io multiplexing, The signal-driven IO is synchronous io, if not blocking, but the operating system to help you do the IO operation and return the results to you, then the asynchronous IO. The difference between blocking IO and non-blocking IO is that the first step is whether the initiating IO request will be blocked, and if blocking until done is the traditional blocking IO, and if it does not block, then it is non-blocking IO.
Received the operating system IO model, but also have to mention SELECT/POLL/EPOLL/IOCP, about the four understanding, not much to explain, I have not understood in place.
Understandably, after Linux 2.6, the implementation of Java NIO is achieved through Epoll, which can be found through the source code of the JDK. AIO, which is implemented through IOCP on Windows, is implemented by Epoll on Linux.
One thing to emphasize here: AIO, which is the I/O processing mode, and Epoll is a programming model for AIO; in other words, AIO is an interface standard that can be implemented or not implemented by each operating system. It is best to use operating system recommendations in high concurrency situations on different operating systems. There is no AIO on Linux that really implements the network way.
https://my.oschina.net/bluesky0leon/blog/132361
Io generally occurs in a read-write hard disk or network file, involving multiple parts of the request call, simply speaking, the request to call the two sides of the processing situation. Blocking non-blocking refers to whether the requester releases the CPU resources when the request is called, or not, if the requester is blocked by the race. Asynchronous synchronization refers to whether the requested party will return the results synchronously or if the request has been accepted, and the result will be communicated to the requester after the request has been processed.
Bio can be thought of as a connection to a thread, while NiO, AIO is a limited request for a connection.