Differences and application scenarios for bio, NIO, and Aio in Java __java

Source: Internet
Author: User
Tags server memory

have been preparing for an interview recently, in order to make their Java level a notch, read the Li Lin Teacher's

"Netty authoritative guide", learned Java about IO development and the latest technology, really benefit, now put

My summary of the differences and application scenarios for bio, NIO and AIO is summarized once again.

Before that, let's find out a few concepts:

1. Synchronization: When using synchronous io, Java handles IO read and write itself.

2. Asynchronous: When using asynchronous IO, Java delegates io Read-write to OS processing, which requires the data buffer address and size to be passed to the OS, and the OS notifies Java processing (callback) after completion.

3. Blocking: When blocking IO is used, Java calls are blocked until read-write is completed before they are returned.

4. Non-blocking: When using non-blocking io, if you cannot read and write immediately, the Java call will return immediately, when the IO event distributor notification can read and write, and continuously loop until read and write.

Here is the key (knocking on the blackboard). )。

1.BIO: Synchronization and blocking, the server's implementation mode is a connection to a thread, such a pattern is very obvious flaw is: Because the number of client connections is proportional to the number of server threads, may cause unnecessary thread overhead, serious will also lead to server memory overflow. Of course, this situation can be improved through the thread pool mechanism, but it does not eliminate this shortcoming in essence.

2.NIO: Before JDK1.4, the Java IO model was always bio, but from JDK1.4, the new IO model NIO introduced by JDK is synchronized non-blocking. The implementation mode of the server is multiple requests for a thread, that is, the request is registered to the multiplexer selector, multiplexer polling to the connection has IO request to start a thread processing.

3.aio:jdk1.7 released the NIO2.0, which is the true meaning of asynchronous non-blocking, the server's implementation mode for multiple valid requests for a thread, the client's IO request by the OS before notifying the server application to start the thread processing (callback).

Application scenario: The number of concurrent connections is not long using bio, because it is very simple to program and debug, but if it involves

and to high concurrency, should choose NiO or AIO, better advice is to adopt a mature network communication framework Netty.


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.