What IO methods are available in Java? What is IO, BIO, NIO, Aio?

Source: Internet
Author: User

IO has been one of the core parts of software development, and with the improvement of Internet technology, IO is becoming more and more important. Throughout the development community, the ability to skillfully use IO is important not only for companies but also for developers. Java's IO mechanism is constantly being perfected to cope with increasing traffic.

Java IO the way

First, the traditional java.io package provides abstraction, input, and output streams such as file. Interactive mode is synchronous, blocking;

Second, the introduction of the NIO Framework (Java.nio package) in Java 1.4 provides abstraction such as Channel,selector,buffer, which constructs multiplexing, synchronous non-blocking IO, and provides a high-performance data manipulation approach that is closer to the underlying operating system;

Thirdly, in Java 7, NIO has been further improved, that is, NiO 2. It introduces an asynchronous non-blocking IO approach, or AIO (asynchronous IO). Asynchronous IO is based on event and callback mechanisms.

Traditional BIO

Traditional bio, a service side that uses the Bio programming communication model, typically consists of a separate acceptor thread that listens for client connections. It receives a client connection request and creates a new thread for each client for link processing, which is returned to the corresponding client through the output stream, and then the thread is destroyed.

The server provides the IP address and the listening port, the client is connected via TCP three handshake, and after the connection succeeds, the two sides communicate through the socket.

The ServerSocket is responsible for binding the IP address, starting the listening port, the socket is responsible for initiating the link operation. The connection is successful, and the two sides are synchronously blocking communication through the input and output streams.

"You came, I just went to" the way of communication.

PS, the API provided by java.net, such as Socket,serversocket,httpurlconnection, also belongs to the synchronous blocking IO class library, because network traffic is also IO behavior.

Advantages: Simple code, intuitive, easy to maintain.

Cons: Very low IO efficiency, impact performance, but rebound processing.

When throughput increases, there is a coup: the thread pool.

Use thread pooling to manage threads, avoid frequent creation, and eliminate the overhead of threading.

However, its underlying use is still synchronous blocking IO, often referred to as the "pseudo-asynchronous IO Model", is really a palliative to the root.

Crossing notice

IO is not only the operation of files, but also the typical IO operation in network programming, such as socket communication.

The input stream (inputstream), the output stream (OutputStream), is used to read or write bytes (ending with a stream in Java).

The reader/writer operation character, added character codec and other functions (in Java with Reader/writer end). Essentially, the computer operates on bytes, whether it is network communication or file reading, and Reader/writer is equivalent to building the bridge between the application logic and the raw data.

Bufferdoutputstream, such as the implementation of buffer, can avoid frequent disk read and write, and thus improve the efficiency of IO processing.

Differentiate between asynchronous and synchronous

synchronization, a reliable and orderly operation mechanism, in the synchronous operation, the subsequent tasks to wait for the current call to return;

Asynchronously, other tasks do not need to wait for the current call to return, depending on the event, callback mechanism to implement the task invocation.

Blocking and non-blocking

Blocked, the current thread is in a blocked state and cannot perform other tasks, only if the condition is ready to continue;

Non-blocking, regardless of the end of the IO operation, directly returned, the corresponding operation in the background to continue processing.

This concludes that synchronous and asynchronous are the results, blocking and non-blocking are the processes.

Nio

New io, or non-block io, non-blocking synchronous mode.

Design principle:

1. The service side communicates with the client through the channel;

2. NiO reads and writes on the channel;

3. The channel is registered on the selector multiplexer. Selector polling these channel through a thread to find the channel that is already ready.

NIO implement non-blocking by polling the thread IO

Components of NiO:

1. Buffer buffers; efficient data containers

The difference is that bio writes the data directly to the Stream object

The data operations of NIO are performed in a buffer.

The buffer is actually an array, common type bytebuffer,charbuffer,shortbuffer,intbuffer,longbuffer,floutbuffer,doublebuffer;

2. The channel is used in NIO to support an abstraction of bulk IO operations, which, unlike streams, are bidirectional.

File/socket, often considered a high-level abstraction, and channel is an abstraction that is more biased towards the bottom of the operating system, which allows NIO to take full advantage of modern operating system underlying mechanisms for performance optimization. Divided into two categories: network read-write Selectablechannel (subclass including Socketchannel and Serversocketchannel); file operations (FileChannel);

3. Selector is the basis for the multi-multiplexing of NIO. It provides an efficient mechanism for continuously polling the channel registered on it, identifying the channel set that is in the ready state, and making subsequent IO operations through the Selectionkey. As long as the server provides a thread responsible for selector polling, it realizes the efficient management of multiple channel with single thread, and is also based on the underlying mechanism of the operating system;

4. Charset provides a Unicode string definition, and NIO also provides the appropriate codec.

Java NIO and the IO One of the biggest differences between: IO is flow-oriented, NIO is buffer-oriented.

AIO ( Asynchronous IO )

NiO 2, presented in Java 7, is an upgraded version of NiO. Implementation of non-blocking asynchronous communication mode;

Provides asynchronous file channels and asynchronous socket channels;

The return type of the Read,write method is the future object;

And the future model is asynchronous, the core idea is: to go to the main function waiting time;

Based on the event and callback mechanism.

Summary

IO, blocking synchronous communication mode, client and server three times handshake, simple, small throughput. Key words: Sockets and ServerSocket;

NIO, non-blocking synchronous communication mode, where the client and the server are connected via channel, using a multiplexer to poll the registered channel. Key words: Socketchannel and Serversocketchannel;

AIO, non-blocking asynchronous communication mode, based on the event and callback mechanism, asynchronous communication is implemented by the async channel. Key words: Asynchronoussocketchannel and Asynchronousserversocketchannel.

Write it in the back.

Key references

Yang Xiaofeng "Java core technology 36 Talk"

Ah Yi "Java Io,bio,nio,aio know how much?" 》

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.