These two articles analyze 5 kinds of IO models under Linux
http://blog.csdn.net/historyasamirror/article/details/5778378
http://blog.csdn.net/hguisu/article/details/7453390
Many people do not understand the concept of blocking/non-blocking, synchronous/asynchronous, do not know the difference between non-blocking and asynchronous Io, and generally think that non-blocking io is asynchronous IO. In fact, the difference is very large, the programming model is completely different .
Blocking/non-blocking describes a function that blocks the thread (block, thread into a blocking state) when accessing a function.
Synchronous/Asynchronous describes who is performing IO operations, and synchronization is performed by the user process itself to perform the final IO operation. Asynchronous is the process of the user process itself does not relate to the actual IO operation, only need to be notified by the kernel after the IO is complete, the kernel process to perform the final IO operation.
The concepts of the non-blocking synchronous IO and non-blocking asynchronous IO, which are all joined together, are not difficult to understand.
non-blocking synchronous IO means that the user invokes a read-write method that is not blocked, returns immediately, and requires a user thread to check IO status. It should be noted that if there is an IO that can be manipulated, the actual user process will still block waiting for the kernel to replicate data to the user process, which is different from the synchronous blocking IO.
non-blocking asynchronous IO refers to the user calls read and write method is not blocked, immediately return, and users do not need to focus on reading and writing, only need to provide callback operations, the kernel thread after the completion of reading and writing callback user-supplied callback.
The difference between the two concepts results in a different programming model .
Non-blocking synchronous IO because the read-write method is non-blocking and requires the user to read and write, the number of bytes actually read and write per call is indeterminate, so a buffer is required to hold the byte state for each read-write process. More importantly, users do not know when to complete the reading and writing, generally need to use while loop to determine the state of the buffer to track reading and writing .
non-blocking Asynchronous IO is read-write by a kernel thread, and when IO is done, the user-supplied callback is recalled, and the programming model is simpler, and the user simply needs to invoke read and write, providing callbacks , such as read (filename, callback)
Select/poll/epoll is essentially non-blocking synchronous io, the Select receives IO-ready status and notifies the user to handle IO, and the actual IO operation requires the user to wait for the kernel to replicate.
to understand the difference between IO readiness and completion . Ready refers to the need for the user to handle, the completion of the kernel is to help complete the user does not care about the IO process, only need to provide callback functions.
Having understood the difference between non-blocking synchronous IO and non-blocking asynchronous IO, it is not difficult to understand the design of Java NIO. NiO is read and write around Bytebuffer, Bytebuffer is a buffer that records the state of Read and write, and checks the state of the Bytebuffer multiple times to determine whether IO is complete .
The NIO2.0 of Java 1.7 introduces the concept of non-blocking asynchronous IO, which simplifies the programming model greatly. The user only needs to focus on the callback function.