1. Early versions of jdk1.4 (jdk1.4 began to provide NIO), Java support for I/O is not perfect, and when developers develop high-performance I/O programs, the main issues are:
There is a problem with I/O performance without buffers
No channel concept, only input/output stream
Synchronous blocking I/O communication (BIO), which usually causes the communication thread to be blocked for a long time
Support for limited character sets and poor hardware portability
2, the Linux kernel will all external devices as a file to operate, read and write to a file will invoke the kernel-provided system commands, return a file descriptor (FD, filename descriptor). and read and write to a socket will also have a corresponding descriptor, called SOCKETFD (Socket descriptor), the descriptor is a number, it points to a structure in the kernel (file path, data area and other properties). Based on UNIX network programming for the I/O model Classification, UNIX provides 5 I/O models, respectively, as follows:
(1) Blocking I/O Model: The most common I/O model is the blocking I/O model, by default all file operations are blocked. We use the socket interface as an example to explain this model: call Recvfrom in process space, its system call knows that the packet arrives and is copied into the buffer of the application process, or an error occurs before returning, during which time it waits. The process is blocked at the start of the call to Recvfrom and the entire time it returns because it is called a blocking I/O model.
(2) Non-blocking I/O Model: Recvfrom from the application layer to the kernel, if the buffer has no data, it will return a ewouldblock error, usually the non-blocking I/O model polling Check this state, see if the kernel has data to come.
(3) I/O multiplexing model: Linux provides select/poll, a process that blocks a select operation by passing one or more FD to a Select or poll system call, so that Select/poll can help us detect if multiple FD is in the ready state. Select/poll is a sequential scan of FD readiness, and the number of supported FD is limited, so its use is subject to some constraints. The biggest drawback of select is that the FD opened by a single process has a certain limit, and the default value is 1024. Linux also provides a epoll system call, and Epoll uses an event-driven approach instead of sequential scanning, so performance is higher. When FD is ready, the callback function callback immediately. The Epoll supported FD cap is the maximum file handle number of the operating system.
(4) Signal-driven I/O Model: first open the socket signal driver I/O function, and through the system call sigaction execute a signal processing function (This system call returns immediately, the process continues to work, it is non-blocking). When the data is ready, a sigio signal is generated for the process, and the signal callback notifies the application to call Recvfrom to read the data and notifies the main loop function to process the data.
(5) asynchronous I/O: tells the kernel to initiate an action and let the kernel notify us when the entire operation is complete, including copying data from the kernel to the user's own buffer area. The main difference between this model and the signal-driven model is that the signal-driven I/O is notified by the kernel when we can start an I/O operation; the asynchronous I/O model is the kernel that notifies us when I/O operations have completed.
3, Epoll on the basis of the improvement of select
(1) Support for a process open socket Descriptor (FD) unrestricted (limited by the maximum number of file handles of the operating system). The default value for the FD supported by select is 1024.
(2) I/O efficiency does not decrease linearly as the number of FD increases
(3) Use MMAP to accelerate message delivery between the kernel and user space
(4) Easy API
4. Related concepts of NIO class library
(1) Buffer buffers
In stream-oriented I/O, data can be written directly to or read directly into a stream object. In the NIO library, all data is processed in a buffer. When the data is read, it is read directly into the buffer and written to the buffer when the data is written. Accessing data in NiO at any time is done through a buffer. The buffer is essentially an array, while the buffer also provides structured access to the data and maintains read-write location (limit) information. The most commonly used buffer is bytebuffer,java each of the basic types (except the Boolean) corresponds to a buffer.
(2) Channel channels
Channels are a channel through which data can be read and written, and it is like a tap, where network data is read or written through the channel. The difference between a channel and a stream is that the channel is bidirectional, the flow is only moving in one direction, and the channel can be used for read, write, or simultaneous reading and writing. Channel can be divided into two categories: Selectablechannel for network reading and writing, and FileChannel for file operation.
(3) Multiplexer Selector
The selector will continually poll the channel registered on it, and if a channel has a new TCP connection access, read, or write event, the channel is in a ready state and will be polled by selector. The Selectionkey can then be used to obtain a set of ready channel, for subsequent I/O operations. A multiplexer selector can poll multiple channel simultaneously.
I/O knowledge