Understanding of Bio,nio,aio in JAVA

Source: Internet
Author: User

Understanding of Bio,nio,aio in JAVA Blog Category:
    • Network programming
"Turn from" http://qindongliang.iteye.com/blog/2018539
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?


Scattered immortal, after checking a part of the information, is willing to try to explain the terms in a way that is easy to understand. If there are deficiencies, also hope to inform.



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. )

First look at the reactor mode, the reactor mode applied to the synchronous I/O scenario. Let's take a read operation and write operation as an example to see the specific steps in reactor:
Read operation:
1. Application registration Read-ready events and associated event handlers

2. Event separators wait for events to occur

3. When a read-ready event occurs, the event splitter invokes the event handler registered in the first step

4. The event handler performs the actual read operation first and then further processes the read-based content

A write operation is similar to a read operation, except that the first step is to register a write-ready event.


Let's take a look at the process of read and write operations in Proactor mode:
Read operation:
1. The application initializes an asynchronous read operation and then registers the appropriate event handler, at which point the event handler does not focus on the read-ready event, but instead focuses on the read completion event, which is the key to distinguish it from the reactor.

2. Event Splitter waits for read operation completion event

3. When the event splitter waits for the read operation to complete, the operating system calls the kernel thread to complete the read operation (the asynchronous IO is the operating system responsible for reading and writing the data to the application to pass in the buffer for application operation, the operating system plays an important role), and the content of the read into the user passed the buffer. This is also a bit different from reactor, where the application needs to pass the buffer proactor.

4. After the event splitter captures the read completion event, activates the event handler registered by the application, and the event handler reads the data directly from the buffer without the need for actual read operations.

Write operations and read operations in Proactor, except that events of interest are write completion events.

As can be seen from the above, the main difference between the reactor and Proactor modes is that the real read and write operations are done by someone, and the reactor requires the application to read or write the data itself, whereas in Proactor mode, the application does not need to perform the actual read/write process. It only needs to read or write from the buffer, and the operating system reads the buffer or writes the buffer to the real IO device.

To sum up, synchronous and asynchronous is relative to the application and the core of the interaction, synchronization needs to proactively ask, and asynchronous when the kernel at the time of the Io event to notify the application, and blocking and non-blocking is only the system calls the system when the function of the implementation of the way.


If you want to have a Kung Pao Chicken bowl:

Synchronization blocking: You go to the restaurant to order, and then wait there, but also shouted: OK!

Synchronous non-blocking: When you finish the meal at the restaurant, walk the dog. But slipped for a moment, and went back to the restaurant and shouted: "Well, no!"

Asynchronous blocking: When you walk the dog, you get a call from the restaurant and say that the meal is ready for you to take it yourself.

Asynchronous non-blocking: the restaurant called and said, we know your location, one will send you over, a safe walk the dog can be.

"An IO operation is actually divided into two steps: initiating IO requests and actual IO operations.
The difference between synchronous IO and asynchronous IO is whether the second step is blocked, and if the actual IO reads and writes blocking the request process, then it is synchronous 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.

Synchronous and asynchronous are for application and kernel interaction, the synchronization refers to the user process trigger IO operation and wait or poll to see if the IO operation is ready, and asynchronous refers to the user process triggered IO operation began to do their own things, and when the IO operation has been completed, the completion of the IO will be notified. While 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 not blocking mode, read or write function immediately return a status value.
Therefore, IO operations can be divided into 3 categories: synchronous blocking (that is, early IO operations), synchronous non-blocking (NIO), asynchronous (AIO).
Synchronous blocking:
In this way, the user process must wait for the IO operation to complete after initiating an IO operation, and the user process will not run until the IO operation is actually completed. Java's traditional IO model belongs to this approach.

Synchronous non-blocking:
In this way, the user process initiates an IO operation to return to doing something else, but the user process needs to ask the IO operation to be ready from time to time, which requires the user process to keep asking, thus introducing unnecessary waste of CPU resources. The current Java NiO belongs to synchronous non-blocking IO.
Asynchronous:
This means that the application initiates an IO operation, does not wait for kernel IO operations to complete, and then notifies the application when the kernel completes the IO operation. ”

This is a very clear statement.


Reference: http://blog.csdn.net/brainkick/article/details/9312407

Understanding of Bio,nio,aio in JAVA

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.