Java NiO series tutorials (1) Java NiO Overview

Source: Internet
Author: User

Original article linkAuthor:Jakob jenkovTranslator:AiruProofread:Ding Yi

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 constitute the core API. Other components, such as pipe and filelock, are only tool classes used together with the three core components. Therefore, in the overview, I will focus on these three components. Other components are described in separate sections.

Channel and Buffer

Basically, all Io operations start from a channel in NIO. The channel is a bit like a stream. Data can be read from the channel to the buffer, or written from the buffer to the channel. Here is an illustration:

There are several types of channels and buffer. Below are some major Channel implementations in Java Nio:

    • Filechannel
    • Datagramchannel
    • Socketchannel
    • Serversocketchannel

As you can see, these channels cover UDP and TCP network Io, and file IO.

There are some interesting interfaces with these classes, but for simplicity, I try not to mention them in the overview. I will explain other topics related to them in this tutorial.

The following are the key buffer implementations in Java Nio:

    • Bytebuffer
    • Charbuffer
    • Doublebuffer
    • Floatbuffer
    • Intbuffer
    • Longbuffer
    • Protocol Buffer

These buffers cover the basic data types that can be sent through IO: byte, short, Int, long, float, double, and char.

Java NIO also has a mappedyteuffer to indicate memory ing files, which I do not intend to explain in the overview.

Selector

Selector allows a single thread to process multiple channels. If your application opens multiple connections (channels), but the traffic for each connection is low, it is very convenient to use selector. For example, in a chat server.

This is an illustration that uses a selector to process three channels in a single thread:

To use selector, you must register a channel with selector and call its select () method. This method will be blocked until a registered channel has an event ready. Once this method is returned, the thread can process these events, such as new connections and data reception.

(Full text)

Reprinted from: http://ifeve.com/overview/

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.