NIO non-blocking package for Android development (5)

Source: Internet
Author: User

Attention and focus on Android NIO today, Android123 focuses on analyzing the shortcomings mentioned in the last step NIO non-blocking package (4) in Android development. As many people in China do not have experience using NIO to implement servers, there are serious errors in many examples, most of the examples are single interactions such as Echo and a small amount of data, which does not reflect the problem.

1. read and Write should be separated. NIO uses an asynchronous method, but it does not mean it will not block. In the above example, we can see the key. when isReadable () is used, do not use the hasRemaining Method for ByteBuffer associated with this SelectionKey when writing too much data. NIO does not necessarily need to read all the data each time it reads and writes.

2. for the above solution, we can continue to pay attention to the events of interest, for example, using the interestOps method, and many documents use the wrong method to continue registering events with the Selector register method, the reason for this error is that the last key registration event will be replaced at the time of each registration, Which is lucky. In terms of efficiency, it will determine what event the key was last registered, it is not an orthodox method.

3. We can continue to judge the write event, such as key. isWritable, to process the sent data when writing. This can cope with exceptions in many low-speed networks.

There are still many details. The next Android Development Network will summarize common problems and give you a more scientific and complete framework to reduce unnecessary troubles.

 

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.