Class SelectableChannel
- All Implemented Interfaces:
Closeable,AutoCloseable,Channel,InterruptibleChannel
- Direct Known Subclasses:
AbstractSelectableChannel
public abstract class SelectableChannel extends AbstractInterruptibleChannel implements Channel
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 SelectionKey.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.
-
Constructor Summary
Constructors Modifier Constructor Description protectedSelectableChannel()Constructs a newSelectableChannel. -
Method Summary
Modifier and Type Method Description abstract ObjectblockingLock()Gets the blocking lock which synchronizes theconfigureBlockingandregistermethods.abstract SelectableChannelconfigureBlocking(boolean block)Sets the blocking mode of this channel.abstract booleanisBlocking()Indicates whether this channel is in blocking mode.abstract booleanisRegistered()Indicates whether this channel is registered with at least one selector.abstract SelectionKeykeyFor(Selector sel)Gets this channel's selection key for the specified selector.abstract SelectorProviderprovider()Gets the provider of this channel.SelectionKeyregister(Selector selector, int operations)Registers this channel with the specified selector for the specified interest set.abstract SelectionKeyregister(Selector sel, int ops, Object att)Registers this channel with the specified selector for the specified interest set and an object to attach.abstract intvalidOps()Gets the set of validoperationsof this channel.Methods inherited from class java.nio.channels.spi.AbstractInterruptibleChannel
begin, close, end, implCloseChannel, isOpen
-
Constructor Details
-
SelectableChannel
protected SelectableChannel()Constructs a newSelectableChannel.
-
-
Method Details
-
blockingLock
Gets the blocking lock which synchronizes theconfigureBlockingandregistermethods.- Returns:
- the blocking object as lock.
-
configureBlocking
Sets the blocking mode of this channel. A call to this method blocks if other calls to this method or to aregistermethod 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-truefor setting this channel's mode to blocking,falseto set it to non-blocking.- Returns:
- this channel.
- Throws:
ClosedChannelException- if this channel is closed.IllegalBlockingModeException- ifblockistrueand this channel has been registered with at least one selector.IOException- if an I/O error occurs.
-
isBlocking
public abstract boolean isBlocking()Indicates whether this channel is in blocking mode.- Returns:
trueif this channel is blocking, undefined if this channel is closed.
-
isRegistered
public abstract boolean isRegistered()Indicates whether this channel is registered with at least one selector.- Returns:
trueif this channel is registered,falseotherwise.
-
keyFor
Gets this channel's selection key for the specified selector.- Parameters:
sel- the selector with which this channel has been registered.- Returns:
- the selection key for the channel or
nullif this channel has not been registered withsel.
-
provider
Gets the provider of this channel.- Returns:
- the provider of this channel.
-
register
public final SelectionKey register(Selector selector, int operations) throws ClosedChannelExceptionRegisters 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 theinterest setis updated tooperations. 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(booleanmethod 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- the selector with which to register this channel.operations- this channel'sinterest set.- Returns:
- 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
public abstract SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelExceptionRegisters 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 itsinterest setis updated toopsand the attached object is updated toatt. 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- the selector with which to register this channel.ops- this channel'sinterest set.att- the object to attach, can benull.- Returns:
- the selection key for this registration.
- Throws:
ClosedChannelException- if this channel is closed.IllegalArgumentException- ifopsis 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
public abstract int validOps()Gets the set of validoperationsof this channel. Instances of a concrete channel class always return the same value.- Returns:
- the set of operations that this channel supports.
-