Name

SelectableChannel

Synopsis

This abstract class defines the API for channels that can be used with a Selector object to allow a thread to block while waiting for activity on any of a group of channels. All channel classes in the java.nio.channels package except for FileChannel are subclasses of SelectableChannel.

A selectable channel may only be registered with a Selector if it is nonblocking, so this class defines the configureBlocking( ) method. Pass false to this method to put a channel into nonblocking mode, or pass true to make calls to its read( ) and/or write( ) methods block. Use isBlocking( ) to determine the current blocking mode of a selectable channel.

Register a SelectableChannel with a Selector by calling the register( ) method of the channel (not of the selector). There are two versions of this method: both take a Selector object and a bitmask that specifies the set of channel operations that are to be “selected” on that channel. (see SelectionKey for the constants that can be OR-ed together to form this bitmask). Both methods return a SelectionKey object that represents the registration of the channel with the selector. One version of the register( ) method also takes an arbitrary object argument which serves as an “attachment” to the SelectionKey and allows you to associate arbitrary data with it. The validOps( ) method returns a bitmask that specifies the set of operations that a particular channel object allows to be selected. The bitmask passed to register( ...

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.