Introduction to AIO, BIO, NiO in Java network IO programming

Source: Internet
Author: User


AIO (asynchronous blocking IO) asynchronous blocking IO

NIO is a synchronous IO, because the program requires IO when it needs to get the IO permission to do the IO operation in person before the next operation. AIO is an improvement on NIO (so Aio is also called nio.2), which is based on the Proactor model. Each socket connection registers an IO completion event and an IO completion event handler in the event splitter. When the program requires IO, it sends an IO request to the splitter and informs the separator of the buffer area used, the separator notifies the operating system to perform IO operations, the operating system constantly attempts to acquire IO privileges and IO operations (data is kept in the buffer area), the separator is notified when the operation is completed, and the separator detects Io completion event, the processor notifies the program that "IO is complete", and the program knows that the data is read and written directly from the buffer area when the IO completion event handler is activated.

In other words:AIO is issued IO request, by the operating system to obtain the IO permissions and IO operations; NIO is the IO request, the thread is constantly trying to get the IO permissions, get to notify the application itself to do IO operations.

BIO (Blocking io) synchronous blocking IO

Our well-known socket programming is bio, a socket connection to a processing thread (this thread is responsible for a series of data transfer operations for this socket connection). The reason for blocking is that the number of threads allowed by the operating system is limited, and when multiple socket requests are connected to the server, the server cannot provide a corresponding number of processing threads, and connections that are not assigned to the processing thread will block wait or be rejected.

NIO (New io) synchronous non-blocking IO

New io is an improvement on bio, based on the reactor model. We know that a socket connection only happens when the data transfer IO operation is characteristic, most of the time this "channel" is idle, but still occupies the thread. The improvement made by NIO is "a request for a thread", in many sockets connected to the server, only the IO operation is required to get the processing thread of the server to IO. This will not limit the socket access because the thread is not enough. When the client socket is connected to the server, an IO request event and IO event handler are registered in the event splitter. When the IO request occurs on the connection, the IO event handler initiates a thread to process the IO request, continuously attempts to gain access to the system's IO and, once successful (i.e., IO), notifies the socket for IO data transfer.

NIO also offers two new concepts: Buffer and Channel

Buffer:
– is a contiguous block of memory.
– Is the transfer of NIO data to read or write.
Channel:
– The source of the data or the destination of the data
– The only interface for buffer objects that provide data to buffer or read buffer data.
– Asynchronous I/O support
Buffer is used as the buffers for the data in the IO stream, while the channel is the IO stream of the socket and the transmit channel of buffer. The IO transfer between the client socket and the server socket does not give the data directly to the CPU,
Instead, the data is saved to buffer via channel channels, and the CPU reads and writes data directly from the buffer area, and reads and writes more content at a time.
The reason for increasing IO efficiency using buffer (here is the same principle as the Bufferedxxstream, BufferedReader, and bufferedwriter in IO flow): The time spent on Io is mainly on the way of data transmission, Normal IO is a byte-to-byte transmission,
In the case of buffer, the transfer of "one chunk of bytes" can be realized by means of buffer encapsulation (such as one-time reading of a row, then transmission in the unit of behavior instead of one byte at a time). For example: IO is to send Express, ordinary IO is a courier run a trip, using the buffer io is a car run a trip. It's clear that buffer is more efficient, and it's spent on the transmission path
Time is greatly shortened.

Article reference link:http://www.cnblogs.com/ygj0930/p/6543960.html

Introduction to AIO, BIO, NiO in Java network IO programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.