Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 453
نمايش فراداده

SelectableChanneljava.nio.channels

Java 1.4closeable

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.

Note that SelectableChannel does not define a deregister( ) method. Instead, to remove a channel from the set of channels being monitored by a Selector, you must call the cancel( ) method of the SelectionKey returned by register( ).

Call isRegistered( ) to determine whether a SelectableChannel is registered with any Selector. (Note that a single channel may be registered with more than one Selector.) If you did not keep track of the SelectionKey returned by a call to register( ), you can query it with the keyFor( ) method.

See Selector and SelectionKey for further details on multiplexing selectable channels.

Figure 13-37. java.nio.channels.SelectableChannel

public abstract class

SelectableChannel extends java.nio.channels.spi. AbstractInterruptibleChannel implements Channel { // Protected Constructors protected

SelectableChannel ( ); // Public Instance Methods public abstract Object

blockingLock ( ); public abstract SelectableChannel

configureBlocking (boolean

block ) throws java.io.IOException; public abstract boolean

isBlocking ( ); public abstract boolean

isRegistered ( ); public abstract SelectionKey

keyFor (Selector

sel ); public abstract java.nio.channels.spi.SelectorProvider

provider ( ); public final SelectionKey

register (Selector

sel , int

ops ) throws ClosedChannelException; public abstract SelectionKey

register (Selector

sel , int

ops , Object

att ) throws ClosedChannelException; public abstract int

validOps ( ); }

Subclasses

java.nio.channels.spi.AbstractSelectableChannel

Returned By

SelectionKey.channel( ), java.nio.channels.spi.AbstractSelectableChannel.configureBlocking( )