Java IO Programming Full solution (i)--java I/O evolution path

Source: Internet
Author: User

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

Earlier versions prior to JDK1.4, Java's support for I/O was imperfect, and developers faced some great challenges and difficulties in developing high-performance I/O programs, the main issues being as follows:

    • There is no data buffer, there is a problem with I/O performance;
    • There is no channel concept in C or C + +, only input and output streams;
    • Synchronous blocking I/O communication (BIO), which usually causes the communication thread to be blocked for a long time;
    • The supported character sets are limited and hardware portability is not good.

For a long time before Java supported asynchronous I/O, high-performance service-side development has long been dominated by C + + and C, and Java's synchronous blocking I/O has been criticized by everyone.

Before JDK1.4 introduced Java NIO, all socket communications based on Java used synchronous blocking mode (BIO), a communication model that responded with a request to simplify application development on the upper level, but there was a huge bottleneck in performance and reliability. As a result, large application servers have been developed in C or C + + languages for a long time because they can use the asynchronous I/O or AIO capabilities provided by the operating system directly. With the increase of concurrent traffic and the increase of response time, the server software developed by Java Bio can only meet high concurrency and delay through the continuous expansion of hardware, which greatly increases the cost of enterprises, and with the expansion of cluster scale, the maintainability of the system also faces great challenges. The problem can only be solved by purchasing a higher-performance hardware server, which can lead to a vicious cycle.

It is the poor performance of Java's traditional bio that makes Java's support for nonblocking I/O more and more, and finally, the JDK1.4 version provides a new NIO class library, and Java can finally support nonblocking I/O.

  JDK1.4 launches NIO 1.0. The new Java.nio package provides many APIs and class libraries for asynchronous I/O development, the main classes and interfaces are as follows.

    • Buffer Bytebuffer for asynchronous I/O operations, etc.;
    • Pipeline pipe for asynchronous I/O operation;
    • Channel for various I/O operations (asynchronous or synchronous), including Serversocketchannel and Socketchannel;
    • Coding ability and decoding ability of multiple character sets;
    • Multiplexer selector for non-blocking I/O operations;
    • A regular expression class library based on popular Perl implementations;
    • File Channel FileChannel.

The provision of the new NIO class library has greatly facilitated the development and application of asynchronous non-blocking programming based on Java, but it still has imperfect place, especially the processing ability of the file system is still insufficient, the main problem is as follows.

    • No unified file attributes (such as read-write access);
    • API capabilities are weaker, such as cascading creation of directories and recursive traversal, which often requires their own implementation;
    • Some advanced APIs for the underlying storage system are not available;
    • All file operations are synchronous blocking calls, and asynchronous file read and write operations are not supported.

  JDK1.7 launches NIO 2.0. The following three improvements are mainly provided.

    • Provides APIs that can obtain file attributes in bulk, which are platform agnostic and not coupled to the file system of the feature, and also provide the SPI for the standard file system for each service provider to extend the implementation;
    • Provides AIO functionality to support file-based asynchronous I/O operations and asynchronous operations for network sockets;
    • Complete the channel functionality defined by the JSR, including support for configuration and multicast datagrams, and more.

So far, we have a more intuitive understanding of Java I/O evolution. The blocking I/O and non-blocking I/O are explained in detail later, along with a code example. We can have a deeper understanding of the drawbacks of traditional blocking I/O and the advantages of non-blocking I/O.

Java IO Programming Full Solution (ii)--traditional bio programming

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

Java IO Programming Full solution (i)--java I/O evolution path

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.