Home  >  Article  >  Java  >  The difference between java nio and io

The difference between java nio and io

王林
王林Original
2019-11-09 16:57:592431browse

The difference between java nio and io

The main differences between IO and NIO in Java are as shown in the figure:

The difference between java nio and io

1. Stream-oriented and buffer-oriented

IO is stream-oriented, and NIO is buffer-oriented.

Java IO is stream-oriented meaning that one or more bytes are read from the stream at a time until all bytes are read, and they are not cached anywhere.

Java NIO's buffer-oriented approach is slightly different. The data is read into a buffer that it processes later, moving back and forth in the buffer as needed.

2. Blocking and non-blocking IO

The various streams of Java IO are blocking. When a thread calls read() or write(), the thread is blocked until some data is read, or the data is completely written.

The non-blocking mode of Java NIO allows a thread to send a request to read data from a certain channel, but it can only get the currently available data. If no data is currently available, nothing will be obtained. Instead of keeping the thread blocked, the thread can continue to do other things until the data becomes readable.

3. Selector

Java NIO's selector allows a single thread to monitor multiple input channels. You can register multiple channels using one selector. A separate thread is then used to "select" channels that already have input that can be processed, or select channels that are ready to be written to. This selection mechanism makes it easy for a single thread to manage multiple channels.

Recommended tutorial: java tutorial

The above is the detailed content of The difference between java nio and io. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn