Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

13. java.nio and Subpackages > SelectableChannel




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( ) may only contain bits that are set in this validOps( ) value.


You are currently reading a PREVIEW of this book.


Get instant access to over $1 million worth of books and videos.


Start a Free Trial

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint