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:

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?

Let's start with an example of life:

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.

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.

1. Synchronization: Refers to the user process triggering IO operation and waiting or polling to see if the IO operation is ready. He went to the streets to buy clothes, himself to do this matter, other things can not be done.

2. Async: 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 the file descriptor, if there is nothing to read, or temporarily not writable, the program enters the waiting state, until there is something to 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 of business, collect a small ticket, after we can play mobile phone, or chat with others, when the wheel we, the bank's horn will be notified, this time we can go.

An IO operation is actually divided into two steps: initiating an IO request and the actual IO operation.

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 bio operations), synchronous non-blocking (NIO), asynchronous non-blocking (AIO).

Sync blocking (BIO):

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 (NIO):

In this way, the user process initiates an IO operation to return to do other things, but the user process needs to ask from time to time whether the IO operation is ready, which requires the user process to constantly ask, thus introducing unnecessary waste of CPU resources. The current Java NiO belongs to synchronous non-blocking IO.

Asynchronous non-blocking (AIO):

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.

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 on the multiplexer, and the multiplexer polls to connect 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): Java Learning Group 669823128

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.

Java Learning Group 669823128

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.