Home >Java >javaTutorial >java NIO tutorial

java NIO tutorial

伊谢尔伦
伊谢尔伦Original
2016-11-26 09:51:481112browse

NIO is the abbreviation of New I/O. To understand its true connotation, you need to master a lot of knowledge. I have tried hard to outline the entire io in these notes. Paving the way for everyone’s in-depth learning.

A brief history of I/O

If you want to understand all of I/O, you must understand the I/O history of java. The I/O history of Java also reflects the development history of Java from one side.

JDK1.0-1.3

In this period of java, it can basically be said that there is no complete I/O support. Because Java I/O operations during this period are blocking, the I/O efficiency is relatively low. Basically, if you want to have a better I/O solution, you basically have to rely on yourself. During this period, Java has not been reused on the server side, which has a lot to do with poor I/O efficiency. Not only was the I/O not done well, but a series of peripheral measures were also not done well. The supported character set encodings are limited, and manual encoding work is often required. And without regular expressions, it is very difficult to process data.

JDK1.4-1.6

In java1.4 released in 2002, non-blocking I/O was added to the java language as JSR-51. At the same time, the encoding and decoding capabilities of the character set have been greatly improved. And there is a regular expression class library based on perl. At the same time, some of the underlying implementations of old I/O have also been rewritten using new I/O methods, which has improved the performance of old I/O. Finally java became popular on the server side.

At the same time, third parties have also begun to exert their efforts. Google released the Guava class library, the I/O part of which greatly simplifies some file operations and data transmission. At the same time, the nio framework netty and mina written under the leadership of Trustin Lee have also been widely circulated, which has greatly promoted the development of java nio.

JDK1.7 to present

With the launch of JSR-203, we saw NIO2 in java1.7. It provides us with more powerful asynchronous I/O operation capabilities that are non-blocking, and also provides a series of extremely convenient APIs for operating file systems and file attributes. And more powerful network I/O

I/O difference

What are the differences between blocking I/O, non-blocking I/O, and asynchronous I/O? Why does every advancement lead to a great improvement in Java I/O capabilities? Let’s take an example of a vegetable growing game.

Suppose there is a vegetable growing game (similar to the previous QQ Farm). After the player plants vegetables, he must stay on that webpage and watch the vegetables mature before he can harvest them. This is an extremely waste of time and the user experience will definitely not be good. This game was later revised. Players no longer need to stay on that web page after planting vegetables. They just need to check it again from time to time. If they find that the vegetables are mature during a certain check, they can harvest the vegetables. Of course, the user experience has been greatly improved and the time wasted by users has also been reduced. However, in order to further enhance the user experience, the game has been revised. After the player plants vegetables, he no longer needs to check whether the vegetables are ripe. When the vegetables are mature, the game will automatically send a notification to the user, telling him that the vegetables are ripe and he should come and collect them quickly. In this way, users basically no longer have to waste time.

The three game versions in the example just represent three types of I/O. Blocking I/O: The CPU cannot proceed with the next operation until the data is read and written, so the CPU has no choice but to wait there. Non-blocking I/O: The CPU can leave before the data is read or written, and it only needs to be asked every once in a while. Asynchronous I/O: Before the data is read and written, the CPU can leave without having to worry about I/O from time to time. When the data is read and written, the CPU is actively notified. The efficiency between the three types of I/O can be judged based on whether it is high or low.

New I/O

Let’s take the non-blocking I/O proposed by java1.4 as the starting point and start to understand the whole picture.

Channels

Buffers

Selectors

These three classes constitute the core API of non-blocking I/O.

Buffer is translated as buffer area, which is a piece of memory that can store data. Channel is a bit like a stream, but it can be read and written, from local I/O to network I/O. Most NIO starts from a Channel. Data can be read from the Channel into the Buffer, or written from the Buffer. to Channel.

java NIO tutorial

In non-blocking I/O, the CPU can leave before the data is read or written, and it only needs to be asked every once in a while. Asking whether the data is read and written requires minimal CPU power, but if the CPU frequently switches tasks, the time required to retain the scene and restore the scene is large. So you can just use a thread to ask if the data is ready. If a thread asks whether the data is ready in multiple channels, it needs to manage multiple channels. This is

java NIO tutorial

To use Selector, you must register the Channel with the Selector and then call its select() method. This method will block until a registered channel has an event ready. Once this method returns, the thread can handle these events.

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