Home  >  Article  >  Java  >  The difference and applicable scenarios between NIO and IO in Java

The difference and applicable scenarios between NIO and IO in Java

伊谢尔伦
伊谢尔伦Original
2017-04-29 12:54:462510browse

NIO was born to make up for the shortcomings of IO operations. Some of NIO's new features are: non-blocking I/O, selectors, buffers and pipes. Channel, Buffer, and Selector are its main features.

Concept explanation:

Channel - Pipes are actually like streams in traditional IO. All data to any destination (or from anywhere) must pass through a Channel object. A Buffer is essentially a container object.

Selector——Selector is used to listen to events of multiple pipes. When using traditional blocking IO, we can easily know when reading and writing can be performed. However, using non-blocking channels, we need some methods to know what When the channel is ready, the selector was born for this need.

What is the difference between NIO and traditional IO?

1. IO is stream-oriented, and NIO is block (buffer)-oriented.

IO stream-oriented operations process data one byte at a time. An input stream produces one byte of data, and an output stream consumes one byte of data. , resulting in poor data reading and writing efficiency;

NIO block-oriented operations generate or consume a data block in one step. Processing data in chunks is much faster than processing data in (streaming) bytes, while the data is read into a buffer that it processes later, and can be moved back and forth in the buffer as needed. This increases flexibility in processing. In layman's terms, NIO adopts a "pre-reading" method. When you read a certain part of the data, it will guess the data you may read next and buffer it in advance.

2, IO is blocking, NIO is non-blocking.

For traditional IO, when a thread calls read() or write(), the thread is blocked until some data is read, or the data is completely written. The thread cannot do anything else during this period.

For NIO, a thread is used to send a read data request. Before getting a response, the thread is idle. At this time, the thread can perform other tasks instead of just waiting for the response to be completed like in IO. .

NIO and IO applicable scenarios

NIO was born to make up for the shortcomings of traditional IO, but it has shortcomings and strengths. NIO also has shortcomings because NIO is buffer-oriented. Operation, every data processing is performed on the buffer, then there will be a problem. Before data processing, it must be judged whether the data in the buffer is complete or has been read. If not, it is assumed that the data has only been read. part, then there is no point in processing incomplete data. Therefore, the buffer data must be detected before each data processing.

So what are the applicable scenarios for NIO and IO?

If you need to manage thousands of connections that are opened at the same time, and these connections only send a small amount of data each time, such as a chat server, then using NIO to process the data may be a good choice.

If there are only a small number of connections, and these connections need to send a large amount of data each time, traditional IO is more suitable. Which method to use to process the data needs to be weighed against the response waiting time of the data and the time to check the buffer data.

Popular explanation

Finally, for NIO and traditional IO, there is a vivid example mentioned by a netizen:

The previous stream was always blocked, and a thread only needed to If you perform an operation, other operations will be blocked, which is equivalent to a water pipe without a valve. When you reach out to receive water, no matter whether the water arrives or not, you can only spend time on receiving water (flow).

The addition of nio's Channel is equivalent to adding a faucet (with a valve). Although it can only receive water from one water pipe at a time, it relies on the rotation strategy. When the water volume is not large, the water flowing in each water pipe The water that comes out can be properly received. The key point is to add a water collector, that is, the Selector. He is responsible for coordination. That is, if there is water in any water pipe, at the moment When the water in the water pipe reaches a certain level, make a switch: temporarily close the current faucet and try to open the other faucet (to see if there is water).

When other people need water, they don’t go directly to collect the water, but provide a bucket to the water collector in advance. This bucket is the Buffer. That is to say, although other people may have to wait, they will not wait on site. Instead, they will go home and do other things. When the water is full, the water collector will notify them.

This is actually very close to the reality of the current social division of labor. It is also a very economical way to use existing resources to achieve concurrent effects, rather than doing parallel processing at every turn, although that is the best way. Simple, but also the most wasteful way of

resources.

The above is the detailed content of The difference and applicable scenarios between NIO and IO in Java. 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