Skip to content

Most visited

Recently visited

navigation
Added in API level 1

SelectableChannel

public abstract class SelectableChannel
extends AbstractInterruptibleChannel implements Channel

java.lang.Object
   ↳ java.nio.channels.spi.AbstractInterruptibleChannel
     ↳ java.nio.channels.SelectableChannel
Known Direct Subclasses
Known Indirect Subclasses


A channel that can be used with a Selector. The channel must be registered with a selector by calling one of the register methods, which return a SelectionKey object. In order to deregister a channel from a selector, its selection key must be canceled. This can be done explicitly by calling the cancel() method but it is also done implicitly when the channel or the selector is closed.

A channel may be registered with several selectors at the same time but only once for any given selector.

Summary

Protected constructors

SelectableChannel()

Constructs a new SelectableChannel.

Public methods

abstract Object blockingLock()

Gets the blocking lock which synchronizes the configureBlocking and register methods.

abstract SelectableChannel configureBlocking(boolean block)

Sets the blocking mode of this channel.

abstract boolean isBlocking()

Indicates whether this channel is in blocking mode.

abstract boolean isRegistered()

Indicates whether this channel is registered with at least one selector.

abstract SelectionKey keyFor(Selector sel)

Gets this channel's selection key for the specified selector.

abstract SelectorProvider provider()

Gets the provider of this channel.

final SelectionKey register(Selector selector, int operations)

Registers this channel with the specified selector for the specified interest set.

abstract SelectionKey register(Selector sel, int ops, Object att)

Registers this channel with the specified selector for the specified interest set and an object to attach.

abstract int validOps()

Gets the set of valid operations of this channel.

Inherited methods

From class java.nio.channels.spi.AbstractInterruptibleChannel
From class java.lang.Object
From interface java.nio.channels.Channel
From interface java.nio.channels.InterruptibleChannel
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Protected constructors

SelectableChannel

Added in API level 1
SelectableChannel ()

Constructs a new SelectableChannel.

Public methods

blockingLock

Added in API level 1
Object blockingLock ()

Gets the blocking lock which synchronizes the configureBlocking and register methods.

Returns
Object the blocking object as lock.

configureBlocking

Added in API level 1
SelectableChannel configureBlocking (boolean block)

Sets the blocking mode of this channel. A call to this method blocks if other calls to this method or to a register method are executing. The new blocking mode is valid for calls to other methods that are invoked after the call to this method. If other methods are already executing when this method is called, they still have the old mode and the call to this method might block depending on the implementation.

Parameters
block boolean: true for setting this channel's mode to blocking, false to set it to non-blocking.
Returns
SelectableChannel this channel.
Throws
ClosedChannelException if this channel is closed.
IllegalBlockingModeException if block is true and this channel has been registered with at least one selector.
IOException if an I/O error occurs.

isBlocking

Added in API level 1
boolean isBlocking ()

Indicates whether this channel is in blocking mode.

Returns
boolean true if this channel is blocking, undefined if this channel is closed.

isRegistered

Added in API level 1
boolean isRegistered ()

Indicates whether this channel is registered with at least one selector.

Returns
boolean true if this channel is registered, false otherwise.

keyFor

Added in API level 1
SelectionKey keyFor (Selector sel)

Gets this channel's selection key for the specified selector.

Parameters
sel Selector: the selector with which this channel has been registered.
Returns
SelectionKey the selection key for the channel or null if this channel has not been registered with sel.

provider

Added in API level 1
SelectorProvider provider ()

Gets the provider of this channel.

Returns
SelectorProvider the provider of this channel.

register

Added in API level 1
SelectionKey register (Selector selector, 
                int operations)

Registers this channel with the specified selector for the specified interest set. If the channel is already registered with the selector, the corresponding selection key is returned but the interest set is updated to operations. The returned key is canceled if the channel is closed while registering is in progress.

Calling this method is valid at any time. If another thread executes this method or the configureBlocking(boolean method then this call is blocked until the other call finishes. After that, it will synchronize on the key set of the selector and thus may again block if other threads also hold locks on the key set of the same selector.

Calling this method is equivalent to calling register(selector, operations, null).

Parameters
selector Selector: the selector with which to register this channel.
operations int: this channel's interest set.
Returns
SelectionKey the selection key for this registration.
Throws
ClosedChannelException if the channel is closed.
IllegalBlockingModeException if the channel is in blocking mode.
IllegalSelectorException if this channel does not have the same provider as the given selector.
CancelledKeyException if this channel is registered but its key has been canceled.
IllegalArgumentException if the operation given is not supported by this channel.

register

Added in API level 1
SelectionKey register (Selector sel, 
                int ops, 
                Object att)

Registers this channel with the specified selector for the specified interest set and an object to attach. If the channel is already registered with the selector, the corresponding selection key is returned but its interest set is updated to ops and the attached object is updated to att. The returned key is canceled if the channel is closed while registering is in progress.

Calling this method is valid at any time. If another thread executes this method or the configureBlocking(boolean) method then this call is blocked until the other call finishes. After that, it will synchronize on the key set of the selector and thus may again block if other threads also hold locks on the key set of the same selector.

Parameters
sel Selector: the selector with which to register this channel.
ops int: this channel's interest set.
att Object: the object to attach, can be null.
Returns
SelectionKey the selection key for this registration.
Throws
ClosedChannelException if this channel is closed.
IllegalArgumentException if ops is not supported by this channel.
IllegalBlockingModeException if this channel is in blocking mode.
IllegalSelectorException if this channel does not have the same provider as the given selector.
CancelledKeyException if this channel is registered but its key has been canceled.

validOps

Added in API level 1
int validOps ()

Gets the set of valid operations of this channel. Instances of a concrete channel class always return the same value.

Returns
int the set of operations that this channel supports.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.