Java Iojava IO Classification
- Java BIO: Synchronization and blocking, the server implementation mode for a connection to a thread, that is, the client has a connection request when the server needs to start a thread to process, if the connection does not do anything will cause unnecessary thread overhead, of course, can be improved through the thread pool mechanism.
- Java NIO : Synchronous non-blocking, the server implementation mode for a request for a thread, that is, when a connection is created, do not need a corresponding thread, the connection will be registered to the multiplexer, so all the connections only need a thread to be able to handle, When the multiplexer in this thread is polled, it is discovered that there is a request on the connection to open a thread for processing, that is, a request for a threading pattern. A more important difference between bio and NiO is that when we use bio, we tend to introduce multiple threads, each connecting a single thread, while NIO uses a single thread or uses only a small number of multiple threads, each of which is shared by each connection.
- Java AIO (nio.2) : Asynchronous non-blocking, the server implementation mode is a valid request for a thread, the client's I/O requests are completed by the OS before notifying the server application to start the thread for processing.
noun explanation
- synchronization : Refers to the user process triggered IO operations need to wait or poll to see the IO operation to complete to perform other operations. This is a poor performance, only a few scenarios where data security requirements are high.
- Async : Asynchronous refers to the user process trigger IO operation will start to do their own things, and when the IO operation has been completed will be the completion of the IO notification (asynchronous feature is notification)
- blocking : The so-called blocking means that when trying to read and write to the file descriptor, if there is nothing to read, or temporarily non-writable, the program enters the waiting state until something is readable or writable.
- non-blocking : Non-blocking state, if nothing is readable, or not writable, the read-write function returns immediately without waiting for
Analysis of scenarios for BIO, NIO and AIO
- The bio method is suitable for a small and fixed number of connections , which requires a high level of server resources, and is limited to applications, JDK1.4 the only choice before, but the program is intuitive and easy to understand.
- The NIO approach is suitable for architectures with a large number of connections and short (light-operated) connections, such as chat servers, which are limited to applications, and are more complex to program, and JDK1.4 begin to support.
- AIO mode allows for a number of connections and long-connected (re-operation) of the architecture, such as the album server, full call to the OS to participate in concurrent operations, programming more complex, JDK7 began to support.
The main differences between Java NiO and IO
-
Stream-oriented and buffer-oriented. The first major difference between Java NiO and Io is that IO is stream-oriented and NIO is buffer-oriented. The Java io-oriented stream means that one or more bytes are read from the stream every time, until all bytes are read, and they are not being slowed anywhere. In addition, it cannot move data in the stream back and forth. If you need to move the data read from the stream before and after it, you need to cache it to a buffer first. Java NiO has a slightly different buffer-oriented approach. The data is read to a buffer that it processes later, and can be moved back and forth in the buffer if needed. This increases the flexibility of the process.
-
The various streams that block and non-blocking IO Java io are blocked. This means that when a thread calls read () or write (), the thread is blocked until some data is read, or the data is fully written. The thread can no longer do anything during this time. The non-blocking mode of Java NIO enables a thread to send requests to read data from a channel, but it can only get the data that is currently available, and if no data is available, the thread could continue to do other things. The same is true for non-blocking writes. A thread requests to write some data to a channel, but does not have to wait for it to be fully written, and the thread can do something else at the same time. Threads typically use non-blocking IO idle time to perform IO operations on other channels, so a single thread can now manage multiple input and output channels (channel).
-
Selector (selectors) Java NiO selectors allow a single thread to monitor multiple input channels, you can register multiple channels using a selector, and then use a separate thread to "select" the channel: these channels already have inputs that can be processed, or select the channels that are ready to be written. This selection mechanism makes it easy for a single thread to manage multiple channels.
Resources
- http://bbym010.iteye.com/blog/2100868
- Http://developer.51cto.com/art/201112/307463.htm
- http://ifeve.com/java-nio-vs-io/
Io for Java interview preparation