What is non-blocking io?

Source: Internet
Author: User

The implementation of a NiO is different. The following is a simple example:

Java code
  1. Bytebuffer buffer = bytebuffer. Allocate (48 );
  2. Int bytesread = inchannel. Read (buffer );



Note that the second row reads bytes from the channel to bytebuffer. When this method is returned, you do not know whether all the data you need is in the buffer zone. What you know is that the buffer contains some bytes, which makes processing a little difficult.

The bufferfull () method must track how much data is read into the buffer and return true or false, depending on whether the buffer is full. In other words, if the buffer is ready for processing, it indicates that the buffer is full.

The bufferfull () method scans the buffer, but the status must be the same before the bufferfull () method is called. If no, the data read from the next buffer may not be able to read the correct location. This is not possible, but it is another issue that requires attention.

 

Summary 

NIO allows you to manage multiple channels (network connections or files) with only one (or several) single thread ), however, the price is that parsing data may be more complex than reading data from a blocked stream.

If you need to manage thousands of connections opened at the same time, these connections only send a small amount of data each time, such as chat servers, the server implementing NiO may be an advantage.

 

Channel implementation 

These are the most important channel implementations in Java Nio:

    • Filechannel: read and write data from a file.
    • Datagramchannel: reads and writes data in the network through UDP.
    • Socketchannel: reads and writes data in the network over TCP.
    • Serversocketchannel: It can listen to new TCP connections, just like the web server. A socketchannel is created for each new connection.

What is non-blocking io?

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.