Java, NIO, AIO-1

Source: Internet
Author: User
Tags message queue

Java, NIO, AIO-1Java

Recently the use of TCP communication in the project to complete the command and operation results of the interaction with the typical TCP communication in the C/s architecture, the reason is simple: in the context of low business requirements, the structure is simple, stable and easy to write. However, in the actual deployment of the situation, there has been no reading of the null pointer exception, the supposedly bio-mode development should be blocked until the data read, no reason to write a message queue, using the timer every 1s from the timer to take data, solve the problem. But to think of this form of synchronous blocking, you want to know about other patterns: NIO, AIO. OK, wordy a lot, get to the point:

Basic concepts of IO operations

Before explaining Bio/nio/aio, understand these concepts and principles, what are synchronous, asynchronous, blocking, and non-blocking:

    • Synchronous
      In IO operations, synchronization refers to the user process starting IO operation and waiting or polling to see if the IO operation is ready
    • Asynchronous
      Asynchronous refers to the user process after the start IO operation, then began to do their own things, IO operations by the system to complete, and when the IO operation is completed, the user process will be completed by the system IO notification.
    • Blocking
      Blocking is when the process accesses data (read and write operations), and waits until the data is ready for the next step if the data is not available.
    • Non-blocking
      Non-blocking is when a process accesses data (read and write operations), and the process immediately gets a return value instead of waiting for it to continue.

Summing up synchronous asynchronous is for the interaction of the application and the kernel, and blocking and non-blocking is for the process when accessing the data, depending on the readiness state of the IO operation to take a different way, is a read and write operation implementation mode. At a lower level, synchronization and asynchrony are only related to the state of the process during IO operations, while blocking and non-blocking are two states of a process. Don't confuse these things.

Based on the basic concepts above, a common four IO modes are generated:

    • Synchronous blocking IO
    • Synchronous non-blocking IO
    • Asynchronous blocking IO
    • Asynchronous non-blocking IO

Here are one by one descriptions of these four models:

    • Synchronous blocking IO
      Synchronous blocking IO is the simplest IO model in which the user process must wait for the IO operation to complete after initiating an IO operation, and the user process can only run after the IO operation has actually been completed. Java's traditional IO model belongs to this way!


Synchronous blocking IO

    • Synchronous non-blocking IO
      In this way, after the user process initiates an IO operation, it can 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 poll, repeat the request, which consumes a lot of CPU resources. This model is rarely used in general, but rather the non-blocking IO feature is used in other IO models.


Synchronous non-blocking

    • Asynchronous blocking IO
      This mode refers to an application that initiates an IO operation, does not wait for kernel IO operations to complete, and when the IO operation is actually completed, the application is notified of the completion of the IO operation. This is actually the most important difference between synchronous and asynchronous, synchronization must wait or proactively to ask whether the IO operation is complete, then why is blocking it? Because this is done through a select system call, and the Select function itself is implemented in a blocking way, the two advantage of using the Select function is that it can listen to multiple file handles at the same time, thereby increasing the concurrency of the system.


Nio

As you can see, the user first adds the socket that requires IO operations to the Select, and then blocks the wait for the select system call to return. When the data arrives, the socket is activated and the Select function returns. The user thread formally initiates a read request, reads the data, and continues execution. The advantages of using the Select function are not limited to this. Although the above approach allows multiple IO requests to be processed within a single thread, the process of each IO request is still blocked (blocking on the Select function), and the average time is even longer than the synchronous blocking IO model. If the user thread registers only the socket or IO request that it is interested in, and then does its own thing and waits until the data arrives, it can increase the CPU utilization.

    • Asynchronous non-blocking IO
      This mode, the user process only need to initiate an IO operation and then immediately return, and so on when the real processing of IO operations is completed, the application will be the completion of the IO operation notification, the user only need to process the data to do so, do not need to do the actual IO read and write operations, Because the real IO read and write operation has been completed by the kernel. In the IO multiplexing model, the event loop notifies the user thread of the status of the file handle, and the user thread reads the data and processes the data itself. In the asynchronous IO model, when the user thread receives the notification, the data has been read by the kernel and placed in the buffer area specified by the user thread, and the kernel notifies the user thread to use it directly after the IO is completed.


Aio

The above is a brief introduction of the current server programming common in several IO models, but the actual implementation of these kinds of IO models are several design patterns:

    • c/S mode, which is the classic bio communication model.
    • Reactor mode is the processing model of asynchronous blocking IO
    • Proactor mode is the processing model of asynchronous non-blocking IO

The second article explains the detailed description of these modes!!

Java, NIO, AIO-1

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.