Name

SelectionKey

Synopsis

A SelectionKey represents the registration of a SelectableChannel with a Selector, and serves to identify a selected channel and the operations that are ready to be performed on that channel. After a call to the select( ) method of a selector, the selectedKeys( ) method of the selector returns a Set of SelectionKey objects to identify the channel or channels that are ready for reading, for writing, or for another operation.

Create a SelectionKey by passing a Selector object to the register( ) method of a SelectableChannel. The channel( ) and selector( ) methods of the returned SelectionKey return the SelectableChannel and Selector objects associated with that key.

When you no longer wish the channel to be registered with the selector, call the cancel( ) method of the SelectionKey. isValid( ) determines whether a SelectionKey is still “valid”—it returns true unless the cancel( ) method has been called, the channel has been closed or the selector has been closed.

The main purpose of a SelectionKey is to hold the "interest set” of channel operations that the selector should monitor for the channel, and also the “ready set” of operations that the selector has determined are ready to proceed on the channel. Both sets are represented as integer bitmasks (not java.util.Set objects) formed by OR-ing together any of the OP_ constants defined by this class. Those constants are the following:

OP_READ

In the interest set, this bit specifies an interest in read operations. ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.