Understanding of Bio,nio,aio in JAVA

Source: Internet
Author: User

In the high-performance IO system design, there are several noun concepts that often confuse us. Specific as follows:
Serial number problem
1 What is synchronization?
2 What is async?
3 What is blocking?
4 What is non-blocking?
5 What is sync blocking?
6 What is synchronous non-blocking?
7 What is asynchronous blocking?
8 What is asynchronous non-blocking?


Before we figure out the above questions, we first need to understand what is synchronous, asynchronous, blocking, non-blocking, and only a few of these individual concepts are understood, and then it is relatively easy to understand them in a combination.

1, synchronous and asynchronous are for the interaction of the application and the kernel.

2, blocking and non-blocking is for the process at the time of access to data, according to the readiness of the IO operation to take a different way, white is a read or write operation function implementation, blocking mode read or write function will wait, and non-blocking mode, read or write function will immediately return a status value.

By describing the basic can summarize a short word, synchronous and asynchronous is the purpose, blocking and non-blocking is the implementation way.

Example of number noun explanation
1 synchronization refers to the user process triggering IO operation and waiting or polling to see if the IO operation is ready to go to the streets to buy clothes, do it yourself, other things can not be done.
2 asynchronous asynchronous refers to the user process triggered IO operation began to do their own things, and when the IO operation has been completed will be the completion of the IO notification (asynchronous feature is notification) tell a friend the size of the appropriate clothes, size, color, let a friend entrusted to sell, and then you can do other things. (When using asynchronous Io, Java will delegate IO read-write to OS processing, which requires the data buffer address and size to be passed to the OS)
3 blocking the so-called blocking mode means that when trying to read and write to the file descriptor, if there is nothing to read, or temporarily not writable, the program enters the waiting state, until something can be read or can be written to the bus station recharge, found this time, the clerk is not (may go to the toilet), Then we wait here until the top clerk comes back. (Of course, the real world is not, but it is true in computers.) )
4 Non-blocking non-blocking state, if there is nothing to read, or not to write, read and write function immediately return, and do not wait, the bank in the withdrawal business, collect a small ticket, we can play mobile phone after the collection, or chat with others, when the wheel we, the bank's horn will be notified, this time we can go.


Here we will understand the combination of the IO type, it is much better to understand.

Synchronous blocking IO (JAVA BIO):
Synchronization and blocking, the server implementation mode for a connection to a thread, that is, the client has a connection request when the server needs to start a thread to process, if the connection does not do anything will cause unnecessary thread overhead, of course, can be improved through the thread pool mechanism.

Synchronous non-blocking IO (Java NIO): Synchronous non-blocking, the server implementation mode is a request for a thread, that is, the connection request sent by the client is registered to the multiplexer, and the multiplexer polls to the connection with an I/O request to start a thread for processing. The user process also needs to ask the IO operation to be ready from time to time, which requires the user process to keep asking.

Asynchronous blocking IO (Java NIO):
In this way, after the application initiates an IO operation, does not wait for the kernel IO operation to complete, and so on after the kernel completes the IO operation notifies the application, this is actually the synchronization and the asynchronous most crucial difference, the synchronization must wait or the initiative to ask whether the IO completes, then why say is blocked? Because this is done by a select system call, and the Select function itself is implemented in a blocking way, the advantage of using the Select function is that it can listen to multiple file handles at the same time (if viewed from a UNP perspective, select is a synchronous operation.) Because the process also needs to read and write data after select, it can improve the concurrency of the system!

(Java AIO (nio.2)) asynchronous non-blocking IO:
In this mode, the user process only needs to initiate an IO operation and then return immediately, after the actual completion of the IO operation, the application will get the IO operation to complete the notification, at this time the user process only need to process the data, do not need to do the actual IO read and write operations, Because the actual IO read or write operation has been completed by the kernel.

BIO, NIO, AIO application Scenario analysis:

The bio method is suitable for a small and fixed number of connections, which requires a high level of server resources, and is limited to applications, JDK1.4 the only choice before, but the program is intuitive and easy to understand.

The NIO approach is suitable for architectures with a large number of connections and short (light-operated) connections, such as chat servers, which are limited to applications, and are more complex to program, and JDK1.4 begin to support.

AIO mode allows for a number of connections and long-connected (re-operation) of the architecture, such as the album server, full call to the OS to participate in concurrent operations, programming more complex, JDK7 began to support.

After figuring out the above concepts, we look back at the reactor mode and the Proactor mode.

(In fact, blocking and non-blocking can be understood as the concept of synchronization, for asynchronous, it will not be divided into blocking non-blocking.) For the user process, after receiving the asynchronous notification, directly manipulate the data in the process user-state space well. )

Understanding of Bio,nio,aio in JAVA

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.