Java NiO's Selector

Source: Internet
Author: User

The selector is a concept of Javanio blockbuster; in order to track multiport messages in the old system, a thread is required for each port to be monitored, but with selector it is not necessary, a selector can manage a channel.

The essence of the selector is to make the work of the listener choose to do, and it will execute periodically to get the channels that are registered to him, such as whether the Socketserverchannel has new messages (whether or not they are ready to accept). And then he will put this channel into a set (Selectedkeys), and after that, you can get the channel that you want to handle by the party. This is like Java NiO made a look at the garden grandfather, timed to go in the Strawberry Garden Walk a lap, all the ripe strawberries into the basket, the basket is placed in the door after the harvest, and this time you arrange the staff (Thread) to take the strawberry (eat, deep processing can be OH).

The old model is directly for each strawberry tree (strawberry is the tree on the bar) to arrange a person (thread), to observe regularly, found cooked, and then processed. Is this a high cost? The existing mechanism, if there is no strawberry, I do not need to arrange more manpower to monitor the situation of strawberry, let them (resources) to do more important things;

Then the metaphor above is that the Strawberry orchard is the selector (Selector), and the gardener is the thread that executes the selector.select (). Strawberry is the channel (Selectablechannel), if you want to put strawberries in the strawberry Orchard, need to be planted by strawberry (selectablechannel.register ()); Is Selectedkeys.

Back to the icy display, Selectionkey defined several operations, Read,write,connect and Accept,selectionkey were created from strawberry planting, selectablechannel.register ( Selector sel, int ops, Object att), operations are performed through OPS. In essence, in the registration time has been specified to require the Gardener (Selector.select ()) monitoring content, monitoring content can not be placed on the channel, can not tell the gardener, it is on the strawberry hanging on the small sign (Selectionkey) noted above.

Finally, the staff, if there is a lot of strawberry in the basket, or deep processing time is very long, can be single-threaded processing, may need a line pool for processing, to ensure that the prepared channels can be processed as soon as possible, and do not interfere with each other.

Java NiO's Selector

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.