Java NIO notes (1): NIO Introduction

Source: Internet
Author: User

Java NIO is Java Non-blocking IO (Java Non-blocking I/O), because it is a new set of operation I/O toolkit added after Jdk1.4, therefore, it is generally called Java New IO. NIO is designed to provide I/O throughput, and its superior performance can even be comparable to that of C. NIO achieves Non blocking through the event-driven mechanism in the Reactor mode. What is the Reactor mode? The Reactor is translated into Chinese as a "Reactor", that is, we register the event to the Reactor. When a corresponding event occurs, the Reactor will tell us what happened, we will proceed accordingly based on specific events.

The performance of the program is largely limited by I/O, so many people will say that the performance bottleneck of the program is no more than I/O. The following figure shows the effect of I/O on program throughput.

Visible I/O has a decisive factor in program throughput. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + ybXESU + 907/Signature + 74 dfoyPujrL/J0tTOqsO/Signature + bXEz7XNs7 + qz/Signature +/token + jI + signature + XH + signature/Signature/ tM/Czbw8L3A + CjxwPjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/Collfiles/20140610/2014061009195693.jpg" alt = "\">

Therefore, NIO can read the channel data to the buffer before performing operations, avoiding the performance overhead of reading by byte or row.

NIO has three core modules: Selector, Channel, Buffer, and java. nio. the new character set class under the charsets package is also an important module of nio. However, I personally think it is not the core of NIO, but a tool class for the NIO core class.

Selector allows a single Thread to process multiple channels. Compared with the old IO to create a Thread for each Channel to prevent blocking, the performance is much higher. Only SelectableChannel can be used to register the Selector, A single thread processes multiple channels such

Register the Channel to Selector and call select () in polling. This method will be blocked. When a registered Channel is ready for IO operations, this will return the number of selected keys, in this case, you can obtain the selected key through selectedKeys to perform related IO operations. The SelectionKey is used to connect Selector and Channel. It will be explained later.

Channels include FileChannel, ServerSocketChannel, SocketChannel, and DatagramChannel. These four channels cover file IO, TCP socket IO, and UDP datagram IO.

Buffer mainly includes ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, and writable Buffer, covering the basic data types of IO operations.

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.