Java.nio.channels
Class Selectablechannel
Java.lang.Object Java.nio.channels.spi.AbstractInterruptibleChannel Java.nio.channels.SelectableChannel
-
All implemented interfaces:
-
closeable, Channel, Interruptiblechannel
-
Directly known subclasses:
-
Abstractselectablechannel
-
Selectablechannel
-
Extends Abstractinterruptiblechannel
-
Implements Channel
Multiplexed channels can be implemented via Selector.
For use with selectors, an instance of this class must first be registered through the Register method. This method returns a new Selectionkey object that indicates that the channel has been registered with the selector.
After registering with the selector, the channel remains registered until it logs off . Logoff involves releasing all resources that the selector has allocated to the channel.
You cannot unregister a channel directly, instead you must suppress the key that represents the channel registration. Canceling a key requires that the channel be unregistered during the next selection operation of the selector. The key can be explicitly canceled by calling the Cancel method of a key. All the keys of the channel are implicitly canceled either by invoking the Close method of the channel or by interrupting the thread that is blocking the I/O operation on that channel from closing the channel.
If the selector itself is closed, the channel is logged off, and the key that it registers is immediately invalid.
A channel can only be registered once on any particular selector.
You can determine whether a channel is registered with one or more selectors by calling the Isregistered method.
Multiple concurrent threads can safely use the selectable channels.
Blocking mode selectable channels are either in
Blockingmode, either in
non-blockingMode. In blocking mode, the other I/O operations that are invoked on their channels are blocked before each I/O operation completes. In nonblocking mode, I/O operations are never blocked, and bytes transferred may be less than the number requested, or bytes may not be transmitted at all. You can determine its blocking mode by calling the Isblocking method that selects the channel.
The newly created selectable channel is always in blocking mode. Non-blocking mode is most useful when combined with selector-based multiplexing. Before registering a channel with a selector, the channel must be placed in non-blocking mode and may not be returned to blocking mode before logging off.
-
Start from the following versions:
-
1.4
-
See also:
-
Selectionkey, Selector
Construction Method Summary |
protected |
SelectableChannel() Initializes a new instance of this class. |
Method Summary |
abstract Object |
blockingLock() Gets the object whose configureblocking and register methods implement synchronization. |
abstract SelectableChannel |
configureBlocking(boolean block) Adjusts the blocking mode for this channel. |
abstract boolean |
isBlocking() Determine if each I/O operation on this channel is blocked before it is completed. |
abstract boolean |
isRegistered() Determines whether this channel is currently registered with any selector. |
abstract SelectionKey |
keyFor(Selector sel) Gets a key that represents the registration of a channel to a given selector. |
abstract SelectorProvider |
provider() Returns the provider who created this channel. |
SelectionKey |
register(Selector sel, int ops) Registers this channel with the given selector, returning a selection key. |
abstract SelectionKey |
register(Selector sel, int ops, Object att) Registers this channel with the given selector, returning a selection key. |
abstract int |
validOps() Returns an action set that identifies the operations supported by this channel. |
Methods of inheriting from class Java.nio.channels.spi.AbstractInterruptibleChannel |
begin, close, end, implCloseChannel, isOpen |
Methods of inheriting from class Java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from Interface Java.nio.channels.Channel |
close, isOpen |
Construction Method Details |
Selectablechannel
Selectablechannel ()
-
Initializes a new instance of this class.
Provider
provider ()
-
Returns the provider who created this channel.
-
-
-
Return:
-
Create a provider for this channel
Validops
Validops ()
-
returns an action set that identifies the operations supported by this channel. The members set in this integer value represent exactly the operations that are valid for this channel. For a given specific channel class, this method always returns the same value.
-
-
-
Return:
-
valid operation set
Isregistered
isregistered ()
-
Determines whether this channel is currently registered with any selector. The newly created channel is always unregistered.
Because of the inherent delay between key cancellation and channel logoff, the channel may remain registered for a certain period of time after all keys for a channel have been canceled. When the channel is closed, the channel may remain registered for a certain amount of time.
-
-
-
Return:
-
returns true if and only if this channel is registered
Keyfor
keyfor (Selector Sel)
-
Gets a key that represents the registration of a channel to a given selector.
-
-
-
Return:
Returns
-
the key when this channel is the last channel registered with the given selector, or null if the channel is not currently registered with the selector
Register
Register (Selector sel, int ops, Object att) throws Closedchannelexception
-
-
Registers this channel with the given selector, returning a selection key.
If this channel is currently registered with the given selector, a selection key representing that registration is returned. The associated set of operations for the key is changed to Ops, just as you would call the Interestops (int) method. If the att parameter is not null, the attachment of the key is set to that value. If the key is canceled, Cancelledkeyexception is thrown.
If the channel has not been registered with the given selector, the channel is registered and the resulting new key is returned. The initial set of available operations for the key is ops, and its attachment is att.
This method can be called at any time. If you call this method while another call to this method or the Configureblocking method is in progress, the call will be blocked first before another operation completes. This method will then implement synchronization on the key set of the selector, so if you call this method concurrently with another registration or selection operation that involves the same selector, the call to this method may be blocked.
If this channel is closed when this operation is in progress, the key returned by this method is canceled, so the return key is invalid.
-
-
-
-
-
Parameters:
-
sel
-The selector to which this channel is registered
-the
-
ops
available action set for the resulting key
-the attachment of the
-
att
resulting key, possibly null
-
Return:
-
Represents the key that this channel registers with a given selector
-
Thrown:
-
ClosedChannelException
-
If this channel is closed-if this channel
-
IllegalBlockingModeException
is in blocking mode
-
IllegalSelectorException
-If this channel is not created by the same provider as the given selector
- -If this channel is currently registered with the given selector, but the corresponding key has been canceled
-
IllegalArgumentException
-If a bit of the OPS set does not correspond to an operation supported by this channel, that is, if the set & ~validops ()! = 0
Register
Register (Selector sel, int ops) throws Closedchannelexception
-
-
registers this channel with the given selector, returning a selection key.
Call this handy method in the form of
sc.register (SEL, OPS)
is identical to the following method calls:
sc.register (SEL, OPS, NULL)
-
-
-
-
-
Parameters:
-
sel
-The selector to which to register this channel
-
ops
-The available set of actions for the resulting key
-
Return:
-
Represents the key that this channel registers with a given selector
-
Thrown:
-
ClosedChannelException
-
If this channel is closed-if this channel
-
IllegalBlockingModeException
is in blocking mode
-
IllegalSelectorException
-If this channel is not created by the same provider as the given selector
- -If this channel is currently registered with the given selector, but the corresponding key has been canceled
-
IllegalArgumentException
-If a bit of the OPS set does not correspond to an operation supported by this channel, that is, if the set & ~validops ()! = 0
Configureblocking
configureblocking (Boolean block) Throws IOException
-
Adjusts the blocking mode for this channel.
If this channel is registered with one or more selectors, attempting to put this channel into blocking mode will cause the illegalblockingmodeexception to be thrown.
This method can be called at any time. The new blocking mode only affects I/O operations that originate after this method returns. For some implementations, this may require blocking other operations until all pending I/O operations are complete.
If you call this method while another call to this method or the Register method is in progress, the call will be blocked first before another operation completes.
-
-
-
Parameters:
-
block
-If true, this channel will be placed in blocking mode, and if false, this channel will be placed in nonblocking mode
-
Return:
-
This selectable channel
-
Thrown:
-
ClosedChannelException
-If this channel is closed
-
IllegalBlockingModeException
-if block is true and this channel is registered with one or more selectors
-
IOException
-If an I/O error occurs
Isblocking
isblocking ()
-
Determine if each I/O operation on this channel is blocked before it is completed. The newly created channel is always in blocking mode.
If this channel is closed, the value returned by this method is unspecified.
-
-
-
Return:
-
returns true if and only if this channel is in blocking mode
Blockinglock
Blockinglock ()
-
gets the object whose configureblocking and register methods implement synchronization. This is often useful in adapter implementations that require a specific blocking pattern to be maintained for a short period of time.
-
-
-
Return:
-
blocking mode lock object
Selectablechannel Similar usage
- How to use Selectablechannel and examples under Android
Selectablechannel API Usage