Java IO Programming Full solution (vi)--4 kinds of I/O comparison and selection

Source: Internet
Author: User
Tags epoll

Reprint Please specify source: http://www.cnblogs.com/Joanna-Yan/p/7804185.html

Previous: Java IO programming Full solution (V)--aio programming

In order to prevent ambiguity due to the understanding or inconsistency of some technical concepts and terminology, the terminology or technical terms involved are stated below: If they are inconsistent with the names of some other places, the interpretation shall prevail.

Asynchronous non-blocking I/O

  Many people like to make the NIO framework provided by JDK1.4 an asynchronous non-blocking I/O, but if you strictly differentiate between the UNIX network programming model and the JDK implementation, in fact it can only be called nonblocking I/O and cannot be called asynchronous nonblocking I/O. Prior to the earlier versions of JDK1.4 and 1.5 update10, the JDK selector was based on the Select/poll model, which is non-blocking I/O based on I/O multiplexing technology, not asynchronous I/O. With the JDK1.5 update10 and Linux core2.6 above, Sun optimized the selector implementation, which replaced Epoll with Select/poll at the bottom, with no change in the upper API, which could be considered a one-time optimization of the JDK NiO, But it still hasn't changed the I/O model.

NIO 2.0, provided by JDK1.7, adds an asynchronous socket channel, which is a true asynchronous I/O that can pass a signal variable when an asynchronous I/O operation is completed and the associated method is recalled when the operation is complete, and asynchronous I/O is also called AIO.

The NIO class library supports non-blocking read and write operations, which are asynchronous compared to previous synchronous blocking reads and writes, and the NIO class library supports non-blocking read and write operations, which are a part of the previous synchronous blocking read and write, so many people are accustomed to calling NiO asynchronous non-blocking I/O, This is also followed by a number of books that introduce NIO programming. To suit everyone's habits, NiO is also referred to as asynchronous non-blocking I/O or nonblocking I/O. Please understand that you should not dwell too much on the semantics of some technical terminology.

Multiplexer Selector 

Almost all Chinese technical books translate selector into selectors, but in fact I think this kind of translation is not appropriate, the selector is only literal meaning, can not reflect the function and characteristics of selector.

The key to the implementation of Java NiO described above is the multiplexing I/O technology, where the core of multiplexing is to poll the channel registered on it through selector, and when one or more channel is found to be in a ready state, An I/O operation is performed by returning the selection key collection of the ready channel from the blocking state. Since the multiplexer is the key to NIO implementation of non-blocking I/O, it is mainly implemented through the selector, so here will selector translated as a multiplexer, and other technical books said the selector is the same thing, please understand.

Pseudo-Asynchronous I/O

  The concept of pseudo-async derives entirely from implementation. Before the JDK NiO program became popular, in order to solve the problem that the Tomcat communication thread synchronous I/O caused the business thread to hang, there was a way to make a buffer between the communication thread and the business thread, which was used to isolate the I/O thread from the direct access between the business threads. This way, the business thread is not blocked by the I/O thread. For the backend business side, the message or task is returned after it is placed in the thread pool, and it no longer accesses the I/O thread directly or reads and writes, so it is not blocked by synchronization. A similar design also includes a front-end startup of a set of threads, encapsulating the received client as a task, and putting it on the backend thread pool execution To resolve a connection-first-thread problem. It is customary to call this a pseudo-asynchronous I/O through a thread pool like this, and the official does not have pseudo-asynchronous I/O, please note.

1. Comparison of different I/O models

The different I/O models vary greatly due to the threading model, API, etc., so the usage difference is also very large.

  Actual development, the specific choice of what kind of I/O model or NIO framework, completely based on the actual business application scenarios and performance requirements, if the number of concurrent connections of the client is not many, peripheral docking network element, the server load is not heavy, it is not necessary to choose NiO to do the server; Then consider choosing the appropriate NIO framework for development.

2. Reasons to choose Netty

The development of high-quality NIO programs is not a simple thing, removing the inherent complexity and bugs of NiO, as a NIO server, it needs to be able to handle the network flash, the client's repeated access, the client's security authentication, message encoding and decoding, half-packet read and write, etc. If you do not have enough NIO programming experience to accumulate, the stability of a NIO framework often takes six months or longer. What's worse, once a problem occurs in the production environment, it often leads to the interruption of service calls across nodes, which can cause the entire cluster environment to be unavailable and restart the server, which can cause huge losses.

From the maintainability point of view, because NiO uses an asynchronous non-blocking programming model, and is an I/O thread processing multiple links, debugging and tracking is very cumbersome, especially in production environment, we can not be effective debugging and tracking, often only rely on some log to assist the analysis, positioning difficulty is very large.

2.1 Reasons for not selecting Java native NiO programming

Now we summarize that we do not recommend developers to use the JDK's NIO library directly for development, for the following reasons.

    1. NiO's libraries and APIs are cumbersome to use, and you need to master selector, Serversocketchannel, Socketchannel, Bytebuffer and so on.
    2. Additional skills are required to pave the groundwork, such as familiarity with Java multithreaded programming. This is because NIO programming is designed to reactor mode, you have to be familiar with multithreading and network programming in order to write high-quality NIO programs.
    3. The reliability of the capacity, the workload and difficulty are very large. For example, the client faces the problems of disconnection, network flash, half-packet read and write, failure cache, network congestion and abnormal code stream processing, and the features of NIO programming are relatively easy to develop, but the workload and difficulty of the reliability ability are very large.
    4. JDK NiO bugs, such as the infamous Epoll bug, will cause selector to empty polling, eventually leading to cpu100%, The official claims that the issue was fixed in the JDK1.6 version of Update18, but until the JDK1.7 version of the problem still exists, but the frequency of the bug has decreased a bit, it is not fundamentally resolved.

For these reasons, in most scenarios, you are not advised to use the JDK's NIO libraries directly unless you are proficient in NIO programming or have special needs. In the vast majority of business scenarios, we can use the NIO framework Netty for NIO programming, which can be used both as a client and as a service side, while supporting UDP and asynchronous file transfers, which is very powerful.

2.2 Why Choose Netty

Netty is one of the industry's most popular NIO frameworks, and its robustness, functionality, performance, customisation, and scalability are among the best in its kind, and it has been validated by hundreds of commercial projects, such as the RPC framework for Hadoop Avro using Netty as the underlying communication framework , many other industry-leading RPC frameworks also use Netty to build high-performance asynchronous communication capabilities.

The advantages of Netty are summarized as follows:

    • Easy to use API, low development threshold;
    • Powerful, preset a variety of codec functions, support a variety of mainstream protocols;
    • The ability of customization is strong, and the communication framework can be flexibly expanded by Channelhandler;
    • High performance, compared with other industry mainstream NIO framework, the Netty is optimal.
    • Mature, stable, netty fixes all the JDK NiO bugs that have been discovered, and business developers don't need to worry about nio bugs anymore;
    • The community is active, the version iteration cycle is short, the bugs found can be repaired in time, and more new features will be added;
    • Experienced a large-scale commercial application test, quality is verified. In the Internet, big data, online games, enterprise applications, telecommunications software and many other industries have been successful commercial, proved that it has been fully able to meet the commercial applications of different industries.

Because of these advantages, Netty is becoming the preferred framework for Java NIO programming.

Netty (i)--netty entry procedure

If this article is helpful to you, please give me a reward!

Java IO Programming Full solution (vi)--4 kinds of I/O comparison and selection

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.