Java NIO (vi) Selector

Source: Internet
Author: User

The selector selector is a component that can detect one or more NIO channels in Java NIO and be able to know if the channel is ready for a read-write event. This allows a single thread to manage multiple channel, thus managing multiple network connections.

Why use Selector

For the operating system, context switching between threads is expensive, and each thread consumes the resources of the system, so use selector to manage multiple channel separately

Creation of Selector
Selector Selector = Selector.open ();

Registering a channel with the selector
Channel.configureblocking (false= Channel.register (selector, selectionkey.op_read);

When used with selector, the channel must be in non-blocking mode, which means that FileChannel cannot be used with selector. Socket channels are available

The second parameter of the register () method is a "interest collection", meaning what events are of interest when listening to the channel through selector. You can listen to four different types of events, expressed in four constants:

    • selectionkey.op_connect: connection Ready (one channel successfully connected to another server)
    • selectionkey.op_accept: receive Ready (one Serversocketchannel ready to receive new incoming connections)
    • Selectionkey.op_read: read Ready (a data-readable channel)
    • Selectionkey.op_write: write Ready (channel waiting to write data)

The channel triggers an event that indicates that the event is ready. If you are interested in more than one event, you can use a bitwise OR operator to connect the constants:

int Interestset = Selectionkey.op_read | Selectionkey.op_write;

Selectionkey

This object contains some properties:

    • Interest Collection
    • Ready Collection
    • Channel
    • Selector
    • Additional objects (optional)
Interest Collection

The interest collection is the collection of events that you choose to be interested in. Interest collection can be read and written by Selectionkey

int interestset = selectionkey.interestops (); boolean isaccept = (Interest & selectionkey.op_accept) = = Selectionkey.op_accept;
Boolean isconnect = (Interest & selectionkey.op_connect) = = Selectionkey.op_connect;
Boolean isread = (Interest & selectionkey.op_read) = = Selectionkey.op_read;
Boolean iswrite = (Interest & selectionkey.op_write) = = Selectionkey.op_write;

The interest collection and Selectionkey constants can be used to determine whether a certain event is in the interest collection, using the bitwise AND operation

Ready Collection

The Ready collection is a collection of operations that the channel is already prepared for. After a selection (Selection), the Ready set is accessed first.

int readyset = Selectionkey.readyops ();

You can detect which events or actions in the channel are ready, as is the case with the interest collection, or in the following ways:

boolean isaccept = selectionkey.isacceptable (); Boolean isconnect = Selectionkey.isconnectable ();
Boolean isread = Selectionkey.isreadable ();
Boolean iswrite = Selectionkey.iswriteable ();

Channel and Selector
Channel Channel == Selectionkey.selector ();

Attaching objects

You can attach an object or more information to a selectionkey, which makes it easy to identify a channel. For example, you can attach a buffer that is used with a channel, or an object that contains aggregated data

= Selectionkey.attachment ();

You can also use the register () method to register a channel with the selector to attach the object

Selectionkey Selectionkey = Channel.register (selector, Selectionkey.op_read, object);

Channel selection via Selector

Once you have registered one or more channels with selector, you can invoke several overloaded select () methods. These methods return the channels in which the event you are interested in is ready.

    • int Select ()
    • int Select (Long timeout)
    • int Selectnow ()

The Select () method blocks to at least one channel ready for your registered event

Select (Long Timeout) and select (), except for the longest block of timeout milliseconds

Selectnow () will not block, and no matter what channel is ready to return immediately

The int value returned by the Select () method indicates how many channels are ready, that is, how many channels have become ready since the last time the Select () method was called. If the Select () method is called once, because a channel becomes ready, returns 1, and if the Select () method is called again, 1 is returned if the other channel is also ready. Even if the first ready channel does not do anything, only one channel becomes ready state between each select () method call

Selectedkeys ()

Once the Select () method is called and the return value indicates that one or more channels are ready, you can then call selector's Selectedkeys () method to access the ready channel in the selected keyset (selected key set)

Set Selectedkeys = Selector.selectedkeys ();

When the Channel is registered with selector, the Channel.register () method returns a Selectionkey object. This object represents the channel registered to the selector. These objects can be accessed through the selector Selectedkeys () method

WakeUp ()

A thread is blocked after invoking the Select () method, allowing it to return even if no channel is ready. As long as other threads call the Selector.wakeup () method on the object on which the first thread calls the Select () method, the line routines on the Select () method is immediately returned

If another thread calls the wakeup () method, and the thread calls the Select () method without blocking, the next thread that calls the Select () method wakes up immediately

Close ()

The close () method is closed after selector is exhausted, and all Selectionkey instances registered to selector are not valid, and the channel itself is not closed

Java NIO (vi) 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.