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.