Java I/O programming (I)-evolution of Java I/O

Source: Internet
Author: User

Java I/O programming (I)-evolution of Java I/O

Reprinted please indicate the source: http://www.cnblogs.com/Joanna-Yan/p/7419117.html

In earlier versions of JDK, Java does not fully support I/O, and developers are faced with great challenges and difficulties when developing high-performance I/O programs, the main problems are as follows:

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

For a long time before Java supports asynchronous I/O, the high-performance server development field has been occupied by C ++ and C for a long time, and Java's synchronous blocking I/O has been criticized.

 

Before JDK1.4 launched Java NIO, All Socket communication based on Java adopts the synchronous blocking mode (BIO), which simplifies application development at the upper layer, however, there are huge bottlenecks in performance and reliability. Therefore, for a long time, large application servers are developed in C or C ++, because they can directly use the asynchronous I/O or AIO capabilities provided by the operating system. When the concurrency traffic increases and the response time delay increases, the server software developed using Java BIO can meet high concurrency and latency only through the continuous expansion of hardware, greatly increasing the cost of the enterprise, as the cluster scale expands, the maintainability of the system is also facing a huge challenge. You can only purchase hardware servers with higher performance to solve the problem, which leads to a vicious circle.

It is precisely because of the poor performance of traditional Java BIO that the call for non-blocking I/O in Java is growing. In the end, JDK1.4 provides a new NIO class library, java can finally support non-blocking I/O.

 

  JDK1.4 launches NIO 1.0.The java. nio package is added to provide 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;
  • Pipeline Pipe for asynchronous I/O operations;
  • Channels that perform various I/O operations (asynchronous or synchronous), including ServerSocketChannel and SocketChannel;
  • Encoding and decoding capabilities of multiple character sets;
  • Selector for non-blocking I/O operations;
  • Regular Expression class library based on popular Perl implementations;
  • File channel.

The provision of the new NIO Class Library greatly promotes the development and application of Java-based Asynchronous non-blocking programming. However, it is still imperfect, in particular, the file system's processing capabilities are still insufficient. The main problems are as follows.

  • No unified file attributes (such as read/write permissions );
  • Weak API capabilities, such as hierarchical Directory Creation and recursive traversal, often need to be implemented by yourself;
  • Some advanced APIs of the underlying storage system cannot be used;
  • All file operations are synchronous blocking calls and do not support asynchronous file read/write operations.

  JDK1.7 launches NIO 2.0.It provides the following improvements.

  • Provides APIs that can obtain file attributes in batches. These APIs are platform-independent and are not coupled with the file system. In addition, they also provide SPI for standard file systems, for various service providers to expand;
  • Provides the AIO function and supports file-based asynchronous I/O operations and network socket asynchronous operations;
  • Complete the channel function defined by JSR, including support for configuration and multicast datagram.

So far, everyone has a more intuitive understanding of Java's I/O evolution. Blocking I/O and non-blocking I/O will be explained in detail later, and a sample code will be given. We will be able to have a deeper understanding of the disadvantages of traditional blocking I/O and the advantages of non-blocking I/O.

Full Java IO programming solution (2)-traditional BIO Programming

If this article is helpful to you, please let me know ~

 

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.