"Turn" Java BIO, NIO, AIO cognition

Source: Internet
Author: User

Abstract: About Java io, many of us are stuck in the Java original API some stream above, then mentioned in the network bio, NIO, AIO and other keywords, do you understand the meaning of the word, and its basic principle?

Note: This article is also the move brick transfer from the network, I think this question to three types of IO image to do a good job, so turned out and we learn together.

Original: http://stevex.blog.51cto.com/4300375/1284437

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 synchronous io, Java handles IO read and write itself), queues (the machine without the number), waits (blocks), takes the money (IO operation), leaves (IO operation completed and returned).

    • 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 for withdrawals, you can only wait (when using blocking IO, the Java call will always block until read-write is completed before returning), always on the line.

    • Non-blocking: Into the Bank Hall, Datang manager First take a number, and then sit in the chair to start mobile phone bucket landlord, equal sign broadcast will notify you to handle, not to the number you can't go, you can continue to ask Datang Manager row to No, Datang manager if said not yet you can not go (using non-blocking IO, If you cannot read and write the Java call will return immediately, when the IO event dispatcher will notify the read and write to continue to read and write, continuously loop until read-write 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. At present, the fire of NiO is relatively large, many interviews will ask whether you understand NiO waiting.

    • AIO mode allows for a number of connections and long-connected (re-operation) of the architecture, such as photo albums, audio-visual servers, 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.

"Turn" Java BIO, NIO, AIO cognition

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.