Java's NIO: An overview of the NIO series of Java Tutorials

Source: Internet
Author: User

One: The core components of the Java NiO?
Java NIO consists of the following core components:

==>channels

==>buffers

==>selectors

Although there are many classes and components in Java NIO, in my opinion, Channel,buffer and Selector form the core API. Other components, such as pipe and filelock, are just tool classes that are used in conjunction with three core components. Therefore, I will focus on these three components in the overview. Other components are described in a separate section.

Channel and Buffer

Basically, all IO starts with a channel in NiO. The Channel is a bit like a stream. Data can be read from the channel to buffer, or it can be written from buffer to the channel. Here's a diagram:



Two: What are the channel components of the NIO in Java?


Here are some of the main channel implementations in Java NIO:

==>filechannel

==>datagramchannel

==>socketchannel

==>serversocketchannel

As you can see, these channels cover both UDP and TCP network IO, as well as file IO.

There are some interesting interfaces along with these classes, but for the sake of simplicity, I try not to mention them in the overview. I will explain the other chapters of this tutorial where they relate.




Three: What does the buffer component of the NIO for Java contain?


The following are the key buffer implementations in Java NIO:

==>bytebuffer

==>charbuffer

==>doublebuffer

==>floatbuffer

==>intbuffer

==>longbuffer

==>shortbuffer

These buffer covers the basic data types that you can send via IO: Byte, short, int, long, float, double, and Char.

Java NIO also has a mappedbytebuffer, which is used to represent memory-mapped files, and I do not intend to describe them in the overview.




Four: What are the selector components of the NIO in Java?
Selector allows single-threaded processing of multiple Channel. If your app has multiple connections (channels) open, but the traffic is low for each connection, it's convenient to use selector. For example, in a chat server.

This is the illustration of using a selector to process 3 channel in a single thread:



To use selector, you have to register the channel with selector and then call its select () method. This method will always block to a registered channel with event readiness. Once this method returns, the thread can handle these events, with examples of events such as new connections coming in, data receiving, and so on.










The NIO of Java: An overview of the NIO series of Java tutorials

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.