Java BIO, NIO, AIO learning

Source: Internet
Author: User

Let's take a look at the concept and take bank withdrawals as an example:

    • Sync: Personally take the bank card to the bank to withdraw money (when using sync io, Java handles IO read and write itself).

    • Asynchronous: Entrust a little brother to take the bank card to the bank to withdraw money, and then give you (using asynchronous IO, Java will read and write IO to the OS processing, the data buffer address and size need to pass to the OS (bank card and password), the OS needs to support the asynchronous IO Operation API).

    • Blocking: ATM queues up and withdrawals, you can only wait (when using blocking IO, the Java call is blocked until read-write is completed before returning).

    • Non-blocking: counter withdrawals, take a number, and then sit in the chair to do other things, the equal sign broadcast will notify you to handle, not to the number you can not go, you may keep asking the lobby manager row to no, lobby manager if it is not yet you can not go (using non-blocking IO, if not read and write Java call will return When the IO event dispatcher notifies the reader to read and write, continue to read and write, continuously looping until read-write is completed.

Java support for bio, NIO, AIO :

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

    • 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 the connection with an I/O request to start a thread for processing.

    • Java AIO (nio.2): Asynchronous non-blocking, server implementation mode for a valid request for a thread, the client I/O requests are completed by the OS before notifying the server application to start the thread for processing,

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.

In addition , I/O is the underlying operation, requires operating system support, concurrency also requires operating system support, so the performance of different operating system differences will be more obvious.

"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. ”

Java BIO, NIO, AIO learning

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.