Java NiO Note (i): NiO introduction

Source: Internet
Author: User

Java NIO, Java non-blocking io (Java non-clogging I/O), is often called Java new IO because it is a new set of operational I/O toolkits added after Jdk1.4. NIO is specifically designed to provide I/O throughput. Its superior performance is even comparable to that of C.

NIO is through the reactor mode of event-driven mechanism to achieve non blocking, then what is reactor mode? Reactor translated into Chinese is the "reactor", that is, we register the event to reactor, when the corresponding event occurs, reactor will tell us what happened. We will do the corresponding processing according to the detailed events.

The performance of the program is very much limited by I/O, so there are a lot of people who say that the performance bottleneck of the program is no more than I/O. The following diagram shows the impact of I/O on program throughput

The visible I/O is a decisive factor in the throughput rate of the program.

The old IO interface in Java. The file operation can only be one byte per byte or one line of reading, the socket IO will be blocked, can create a thread for each socket, but this overhead and resource waste is too large, not a reasonable choice, and NIO to socket IO can be non-clogging, Ability to manage multiple channels with a single thread. And NiO has the concept of a buffer, both file IO and socket IO are read in and buffer to each other. See

So NiO was able to read the channel data into the buffer before it could be manipulated. Avoids the performance overhead of byte-by-bit or line-by-row reads.

NIO has three core modules: Selector (selector), channel (channels), buffer (buffers), and the new character set class under Java.nio.charsets package is also an important module of NIO. But personally, it is not the core of NIO, just a tool class for the core class of NiO.

Selector agrees that a single thread handles multiple channel, creating a thread for each channel compared to the old IO to prevent clogging. The performance is much higher. Just use Selectablechannel talent to register to selector. Single-threaded processing for multiple channel

Register the channel with the selector. Polling invokes select (). This method is blocked, and when a channel of the register is ready for IO operation, this returns the number of selected keys, at which point the selected key is obtained by Selectedkeys. can be associated with IO operations; the Select key (Selectionkey) is used to connect the selector and channel. will be in explanation.

Channel mainly include: FileChannel, Serversocketchannel, Socketchannel, Datagramchannel, these four kinds of channels cover the file IO, TCP socket IO, UDP datagram io.

Buffer mainly includes: Bytebuffer, Charbuffer, DoubleBuffer, Floatbuffer, Intbuffer, Longbuffer, Shortbuffer, which covers the basic data types of IO operations.

Java NiO Note (i): NiO introduction

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.