Java NiO learning notes selector behavior mechanism analysis (Select Operation cancel Operation)

Source: Internet
Author: User


Logs originally recorded in blogjavaHttp://www.blogjava.net/wfeng007/archive/2008/04/06/191112.html
 It is mainly used to learn the basic mechanism of selector.

When I learned the NIO tutorial from IBM, I found thatCodeThere was a serious problem.
That is, when the client suddenly interrupts the TCP connection. The server enters a broken infinite loop.

Later it was found that the selector was able to select () The selectedkey of the socketchannel in the op_read status when the TCP connection was disconnected.
In this case, it is determined by the return value when the channel reads data to the buffer.
This reverse return value is actually the number of bytes read. When the value is 0, it indicates that no data can be read. When the value is-1, it indicates that the underlying TCP has been disconnected. (However, the IE connection is a little different. An exception is directly given during read. All of these conditions must be determined .)

Then I thought about how to cancel the association between selector and socketchannel? What is the basic internal operation of selector? The key. Cancel () method is written in javadoc. Socket close () seems to have the same function in windows.

Here we can use two graphs to represent the internal mechanism of several common selector actions.
First, the entire selector contains four key sets or arrays.


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.