Reprint: BIO | NIO | Aio

Source: Internet
Author: User
Tags epoll

http://my.oschina.net/bluesky0leon/blog/132361

Also about Bio | NIO | AIO (Java Edition) reprinted from: Zheng-lee blog

Release time: 2013/05/21 12:56

About Bio | NIO | The discussion of AIO has always been there, and sometimes it is easy to confuse, to give an explanation as to my understanding:

BIO | NIO | AIO, the description of itself is based on the Java language. While describing IO, we need to start with two levels:

    1. Programming languages
    2. Implementation principle
    3. Underlying Foundation
from the programming language level

BIO | NIO | AIO in the Java perspective, understanding, Linux C also has the concept of AIO (library), these concepts do not know what reason is fired up, here only from the Java perspective.

    • BIO, synchronous blocking Io, simple comprehension: One connection to one thread
    • NIO, synchronous non-blocking IO, simple comprehension: one request a thread
    • AIO, asynchronous non-blocking IO, simple comprehension: A valid request for a thread
BIO

Before JDK1.4, writing a network request in Java is to create a serversocket, and then, when the client establishes the socket, it asks if the thread can handle it, or if not, either waits or is rejected. That is: A connection that requires the server to correspond to a processing thread.

Nio

In Java, a set of APIs is provided in JDK1.4 and later versions to specifically manipulate non-blocking I/O, and we can find related classes and interfaces in the Java.nio package and its child packages. Since this API is the new I/O API provided by the JDK, it is also called New I/O, which is the origin of package name NiO. This set of APIs consists of three main components: buffer (buffers), channel (Channels), and non-blocking I/O core classes. In understanding NIO, it is necessary to differentiate between New I/O or non-blocking io,new I/O is a Java package, and NiO is a non-blocking IO concept. This is about the latter one.

NiO itself is based on event-driven thinking, its main solution is the big concurrency problem of bio: In network applications using synchronous I/O, if you want to process multiple client requests at the same time, or if the client wants to communicate with more than one server at the same time, it must be processed using multithreading. That is, each client request is assigned to a thread that is processed separately. While this can meet our requirements, it will also lead to another problem. Since each thread is created, it is necessary to allocate a certain amount of memory (also known as a working memory) to the thread, and the operating system itself has a certain limit on the total number of threads. If the client has too many requests, the server may be overwhelmed to deny requests from the client, or even the servers may be paralyzed.

NIO is based on reactor, when the socket has a stream-readable or writable socket, the operating system notifies the reference program to process it, and the application then reads the stream into the buffer or writes to the operating system. In other words, this time, is not a connection to a processing thread, but a valid request, corresponding to a thread, when the connection has no data, there is no working thread to handle.

Aio

Unlike NiO, when it comes to read and write operations, only the read or write method of the API can be called directly. Both methods are asynchronous, and for a read operation, when a stream is readable, the operating system streams the readable buffer into the Read method and notifies the application that, for a write operation, the operating system proactively notifies the application when it finishes writing the stream passed by the write method. It can be understood that the Read/write method is asynchronous and will invoke the callback function after completion. In JDK1.7, this part of the content is called nio.2, with the following four asynchronous channels added mainly under the Java.nio.channels package:

    • Asynchronoussocketchannel
    • Asynchronousserversocketchannel
    • Asynchronousfilechannel
    • Asynchronousdatagramchannel

The Read/write method, which returns an object with a callback function, calls the callback function directly after the read/write operation is performed.

Implementation Principle

Say the principle of implementation, but also from the operating system IO model to understand

According to the UNIX network programming, the IO model can be divided into: blocking IO, non-blocking IO, IO multiplexing, signal driven IO and asynchronous Io, divided into two categories according to POSIX standard: synchronous IO and asynchronous IO. How do you differentiate it? First IO operation is actually divided into two steps: Initiate IO request and actual IO operation, the difference between synchronous IO and asynchronous IO is whether the second step is blocked, if the actual IO read/write blocking request process, then is synchronous io, so blocking IO, non-blocking IO, io multiplexing, The signal-driven IO is synchronous io, if not blocking, but the operating system to help you do the IO operation and return the results to you, then the asynchronous 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.

Received the operating system IO model, but also have to mention SELECT/POLL/EPOLL/IOCP, about the four understanding, not much to explain, I have not understood in place.

Understandably, after Linux 2.6, the implementation of Java NIO is achieved through Epoll, which can be found through the source code of the JDK. AIO, which is implemented through IOCP on Windows, is implemented by Epoll on Linux.

One thing to emphasize here: AIO, which is the I/O processing mode, and Epoll is a programming model for AIO; in other words, AIO is an interface standard that can be implemented or not implemented by each operating system. It is best to use operating system recommendations in high concurrency situations on different operating systems. There is no AIO on Linux that really implements the network way.

Underlying Foundation

Speaking of the bottom, to say that the Linux system programming, here itself is not familiar with, waiting for Yimeimei to add. Just a general: AIO implementation

On Windows, the AIO implementation is done through IOCP, looking at the source code of the JDK to discover

Windowsasynchronoussocketchannelimpl

See Implementing the Interface:

Implements Iocp.overlappedchannel

Look at the implementation method: The inside of the Read0/write0 method is the native method, invoking the JVM implementation of the underlying, virtual machine technology is not familiar with, not caught dead.

In Linux, the implementation of AIO is through the epoll to complete, look at the JDK source code, you can find that the realization of the source code is:

Unixasynchronoussocketchannelimpl

See Implementing the Interface:

Implements Port.pollablechannel

This is the biggest difference with windows, the implementation of poll, after linux2.6, the default is to use Epoll.

That's understandable.

Written in the end: Java development-based, for the operating system underlying knowledge is not C-based Daniel Good, language determines the way of thinking, the Ancients did not bully me

Finally, a few explanations of the Good article:

BIO NIO AIO

Getting Started with nio.2, part 1th: Asynchronous channel API

Use asynchronous I/O to greatly improve application performance

© Copyright belongs to the author

Reprint: BIO | NIO | Aio

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.