Java I/O summary (ii) NIO

Source: Internet
Author: User

Synchronous asynchronous, blocking non-blocking for I/o:

Blocking: The thread waits when the data required to perform the operation is not ready

Non-blocking: The thread does not wait when the data is not ready

Sync: Perform operations, wait until the operation is finished before performing down

Asynchronous: Performs an operation, does not wait for the interface to be invoked, executes down

the Common read () and write () methods are synchronous I/O.

of traditional I/O is a blocking type.

The problem is that when the data required for the operation is not ready, the thread waits until the data arrives.

in order to solve this problem, we have introduced NIO Concept: Synchronous non-blocking I/O.

NIO is equivalent to defining a scheduler selector, which registers all channel channels that implement communication with the selector, It dispatches the operation of each channel and realizes that the channel does not transmit in blocking.

Channel :

filechannel datagramchannel (UDP) socketchannel (TCP) , serversocketchannel (TCP)

These channels cover the UDP tcp network io io

buffer : (with three parameters, capacity Capacity position Span style= "font-family: Song body", upper bound limit )

  write mode, limit equals capacity ; Read mode, limit equals position

bytebuffer charbuffer DoubleBuffer floatbuffer intbuffer longbuffer , shortbuffer

these Buffer covers the Basic data Typesyou 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.

Selector (can be compared to a Scheduling tools)

Selector allows single-threaded processing of multiple Channel

The only interface for buffer objects that provide data to buffer or read buffer data .

1. Create selector,channel, set channel channel to non-blocking mode

2. bind The socket object to the channel

3. Register the channel with the selector

4. Call selector 's selectedkeys to check all channels if anything is needed to happen, and if something happens, return all the Channel object.

5. through the channel, read the data of the communication, read the buffer.


Java I/O summary (ii) NIO

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.