Java BIO, NIO, AIO Basic concepts

Source: Internet
Author: User

cite other people's examples to understand the concept, taking bank withdrawals as an example.
Synchronous and asynchronous: These two concepts are related to the notification mechanism of the message, that is, in the case of synchronization, it is up to the processing message to wait for the message to be triggered, and in the case of asynchronous, the trigger mechanism notifies the processing message.
① Sync: Personally take the bank card to the bank to withdraw money (when using synchronous io, Java handles IO read and write itself).
② Async: 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 and non-blocking: related to the state when a program waits for a message (no synchronization or asynchronous).
③ blocking: ATM queues and withdrawals, you can only wait for no other action (when using blocking IO, Java calls are blocked until read-write is completed).
③ non-blocking: ATM queued withdrawals, you can call while waiting.

1.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 to the multiplexer, and the multiplexer polls 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,

2.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, is limited to applications, and JDK1.4 the only previous choice, but the program is intuitive and easy to understand.
The ②nio method is suitable for a number of connected connections and relatively short (light operation) of the architecture , such as chat server, concurrent limited application, programming is more complex, JDK1.4 began to support.
The ③aio mode allows for more connections and long connections (re-operation) of the architecture , such as the album server, full call OS to participate in concurrent operations, programming is more complex, JDK7 began to support.

    另外,I/O属于底层操作,需要操作系统支持,并发也需要操作系统的支持,所以性能方面不同操作系统差异会比较明显。

The main differences between 3.Java NiO and IO

IO NIO
Stream oriented Buffer oriented
Blocking IO Non-blocking IO
No Selector Selector

stream-oriented and buffer-oriented
The first major difference between Java NiO and Io is that IO is stream-oriented and NIO is buffer-oriented. The Java io-oriented stream means that one or more bytes are read from the stream every time, until all bytes are read , and they are not being slowed anywhere. In addition, it cannot move data in the stream back and forth . If you need to move the data read from the stream before and after it, you need to cache it to a buffer first. Java NiO has a slightly different buffer-oriented approach. The data is read to a buffer that it processes later, and can be moved back and forth in the buffer if needed. This increases the flexibility of the process. However, you also need to check if the buffer contains all the data that you need to process. Also, make sure that when more data is read into the buffer, do not overwrite the data that has not been processed in the buffer.
blocking and non-blocking IO
The various streams of Java io are blocked . This means that when a thread calls read () or write (), the thread is blocked until some data is read, or the data is fully written. The thread can no longer do anything during this time. the non-blocking mode of Java NiO enables a thread to send requests to read data from a channel, but it can only get the data that is currently available and will not get anything if no data is available. Instead of keeping the thread blocked, the thread can continue to do other things until the data becomes readable. The same is true for non-blocking writes. A thread requests to write some data to a channel, but does not have to wait for it to be fully written, and the thread can do something else at the same time. Threads typically use non-blocking IO idle time to perform IO operations on other channels, so a single thread can now manage multiple input and output channels (channel).
selector (selectors)
The Java NiO selector allows a single thread to monitor multiple input channels , you can register multiple channels using a selector, and then use a separate thread to "select" the channel: these channels already have inputs that can be processed, or select the channels that are ready to be written. This selection mechanism makes it easy for a single thread to manage multiple channels.

Java BIO, NIO, AIO Basic concepts

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.