Java NIO5: Selector 1---theory chapter

Source: Internet
Author: User

Selector Selector

Finally, let's explore the selector. Because the selector content is more, so this article first partial theory to say, after a code, the article does not have any summary, summary, write where to calculate where, only to be able to write the selector to understand, and will be some of the relatively important content bold red.

selectors provide the ability to choose which tasks are already in place, which makes multivariate I/O possible , with ready execution and multivariate selection enabling single threads to efficiently manage multiple I/O channels at the same time.

In some programs, understanding selectors is more difficult and more complicated than understanding buffer and channel classes, because there are three main classes involved, all of which are involved in this whole process, where the execution of the selector is first decomposed into several details:

1. Create one or more selectable channels

2. Register these created channels in the Selector object

3, select the key will remember the developers concerned about the channel, they will also track whether the corresponding channel is ready

4. When the developer invokes the Select () method of a Selector object, the associated key is updated to check all channels registered with the selector

5. Get a collection of keys to find the channel that was already ready, by traversing these keys, the developer can choose the channel from which the Last call to select () is now ready

The understanding of the selector is roughly the number of steps, OK, then go further and look at the three classes associated with the selector.

Selector, selectable channel, and select Key class

Selector (Selector)

The selector class manages the information of a registered channel collection and their readiness state . The channel is registered with the selector, and the selector is used to update the ready state of the channel, and when you do so, you can choose to suspend the fired thread until there is a ready channel.

Selectable channels (Selectablechannel)

This abstract class provides the public method required to implement the selectivity of the channel, which is the parent class of all channel classes that support readiness checks, FileChannel objects are not selectable because they do not inherit selectablechannel, all socket channels are selectable, Includes the channel obtained from the pipe object. The Selectablechannel can be registered to the Selector object, and it can be set to which of the selectors is interested . A channel can be registered to multiple selectors, but only once for each selector .

Select Key (Selectionkey)

The Select key encapsulates the registration relationship of a particular channel with a specific selector . Select Key Object Bai Selectablechannel.register () returns and provides a token that represents this registration relationship. The selection key contains two bit sets (encoded as integers), indicating the channel operations that the registration relationship is concerned about, and the operations that the channel is already ready for.

Use a UML diagram to describe selectors, selectable channels, and select keys:

Setting up selectors

As mentioned earlier, the function of the selector is to manage the set of registered channels and their readiness state, assuming we have a selector for three socket channels, there may be similar code:

=



Ready = Selector.select (10000
...

This operation is represented by a graph:

The code creates a new selector and then registers the four (already existing) socket channels on the selector, and the operations of interest are different. The Select () method puts the thread to sleep until one of these interesting events occurs, or 10 seconds past, which is called event-driven .

Take a look at the API details of selector:

 public  abstract  class   selector{...  public  static     Selector open () throws   IOException;  public  abstract  boolean   IsOpen ();  public  abstract  void  Close () throws   IOException;  public  abstract   Selectionprovider provider (); ...}

Selector is instantiated by calling the static Factory method open (), which is also seen from the previous code . selectors are not basic I/O objects like channels or streams----data never passed through them .

The channel is called register method registered to the selector, from the code can see the register () method to accept a selector object as a parameter, and an integer named Ops parameter, the second parameter represents the channel operation of Concern. In JDK1.4, there are four selectable actions defined: Read, write, connect (connect), and accept.

Note Not all operations are supported on all selectable channels , for example Socketchannel does not support accept.

Using the Select key

Next look at the Select key, the API to select the key is as follows:

 Public Abstract classselectionkey{ Public Static Final intOp_read;  Public Static Final intOp_write;  Public Static Final intOp_connect;  Public Static Final intop_accept;  Public AbstractSelectablechannel channel ();  Public AbstractSelector Selector ();  Public Abstract voidCancel ();  Public Abstract BooleanIsValid ();  Public Abstract intInterestops ();  Public Abstract voidIterestops (intfops);  Public Abstract intReadyops ();  Public Final Booleanisreadable ();  Public Final Booleaniswritable ();  Public Final Booleanisconnectable ();  Public Final Booleanisacceptable ();  Public FinalObject Attach (object OB);  Public FinalObject attachment ();}

As mentioned earlier, a key represents a registration relationship between a particular channel object and a particular selector object, and the Channel () method and the selector () method reflect this relationship.

Developers can use the Cancel () method to end this relationship, use the IsValid () method to check if this valid relationship still exists, and use the Readyops () method to get the operation that the associated channel is ready to take. However, we often do not need to use the Readyops () method, the Selectionkey class defines four easy-to-use Boolean methods to test the state of the channel's readiness for the developer:

if (Key.iswritable ())

This notation is equivalent to:

if ((Key.readyops () & selectionkeys.opwrite)! = 0)

IsWritable (), isreadable (), isconnectable (), Isacceptable () Four methods can be safely invoked on any of the Selectionkey objects.

Java NIO5: Selector 1---theory chapter

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.