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.
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
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.

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools