Transferred from: http://www.molotang.com/articles/894.html
After the Java release of JDK1.4, NiO also came along. the advent of NiO is actually intended to provide more and more flexible interfaces for Java development, but also to improve performance. We have an overview of NIO in this article.
0. Synchronous asynchronous and blocking of IO
Java NiO is Java New IO. But NiO is also usually could be free with bio and AIO, and some people interpret NiO as non-blocking io, non-blocking IO. This understanding is also justified, since NIO makes non-blocking IO possible. Here we have a simple collation of synchronous asynchronous and blocking non-blocking for Io, which is actually two dimensions for IO classification.
Blocking and non-blocking, this is well understood. Blocking is when a thread blocks until the IO request is made, and the thread resumes execution after the IO operation completes. Non-blocking is the initiation of a request, and if there is no result there is no need to wait and return directly.
There are many explanations for synchronous and asynchronous.
One argument is that synchronous asynchrony refers to stateful dependence, which usually refers to doing one thing after another, which means to wait for the first thing to be done before doing the second thing. and async can do other things without thinking about them directly.
There is also a way of saying that communication is done by who is responsible for notifying the actual completion event of IO.
http://www.zhihu.com/question/19732473
Java's traditional stream of byte streams Io is mostly bio, which is blocking IO. In NiO, the channel can use non-blocking IO requests. In Javase 7, some features of AIO have been added.
1. NiO in the Java API
Let's look at the NIO packages in the Java API.
Java NIO API
The Java.nio package consists primarily of the concept definition of the NIO buffer. The Java.nio.channels package is mainly the definition of the channel concept related classes, and the concept of selector is defined in this package. These three are the main roles of Java NIO, in addition to Java NIO, which defines other sub-packages. Many of these places use the SPI, the concept of service Provider interface defines the child package.
In addition, Java NiO contains character set CharSet, regular expressions, and file-handling related content.
2. IO multiplexing
As I understand it, the most appealing feature of Javanio is IO multiplexing.
Before NIO, usually a Java server received a number of client connection requests, and for each request a thread was opened for processing requests. With the popularity of asynchronous requests and long connections, many times a connection remains in the state, but the data requests are small and there is no need for many resources to continue processing the request. The previous pattern of connecting to one thread wastes server resources because the threads need to be resource-intensive.
A more appropriate way is that the connection can be maintained all the time, but the thread that handles the request is used, that is, a thread that handles multiple connections with more scattered requests. This is what we want to say about IO multiplexing.
In NiO, every data connection can correspond to a channel, and I can register a particular interest event for a channel to selector, and the thread that handles the request is just as good as detecting the selector state. When selector receives a specific event, our execution thread can get the relevant channel through this selector and do the appropriate processing. Continue to check selector thereafter. Such a thread can be reused to handle data requests on different channel.
Reference:
http://docs.oracle.com/javase/1.4.2/docs/guide/nio/
The author of the original, inevitably have errors, welcome readers enthusiastic comments message points, so as not to mislead others, thank you!
Talk about Java NIO "Turn"