Java.nio.channels
Class Selectionkey
Java.lang.Object
Java.nio.channels.SelectionKey
-
Directly known subclasses:
-
Abstractselectionkey
Selectionkeyextends Object
Represents the Selectablechannel token that is registered in Selector.
A selection key is created each time a channel is registered with the selector. It remains valid by calling the Cancel method of a key, closing its channel, or canceling the key by closing its selector. Canceling a key does not immediately remove it from its selector; instead, it is added to the deselected keyset of the selector so that it is removed the next time the selection operation occurs. You can test its validity by calling the IsValid method of a key.
The selection key contains two sets of operations represented as integer values. Each bit of the action set represents a class of selectable operations supported by the channel of the key.
The interest collection determines the readiness information for which type of operation will be tested the next time the selection method of a selector is called. Initializes the interest collection with the given value when the key is created, which can then be changed by the interestops (int) method.
The Ready collection identifies a type of operation in which the selector of a key detects that the channel for that key has been prepared for such an operation. The Ready collection is initialized to zero when the key is created, and it can be updated by a selector in a subsequent selection operation, but it cannot be updated directly.
The Ready collection of the Select key indicates that its channel is prepared for an action category, which is only a hint and does not guarantee that the thread can perform operations in this category without causing it to be blocked. The Ready collection is likely to be accurate once the selection operation is complete. The ready collection can become inaccurate due to external events and I/O operations that are invoked on the appropriate channel.
This class defines all known operation set bits (Operation-set bit), but which bits are supported by the given channel depending on the type of the channel. Each subclass of Selectablechannel defines the Validops () method, which returns a collection that exactly identifies the operations supported by that channel. An operation set bit that is not supported by a channel that attempts to set or test a key will cause the corresponding run-time exception to be thrown.
You typically must associate an application-specific data with a selection key, such as an object that represents the state of the advanced Protocol and an object that handles readiness notifications in order to implement the Protocol. Therefore, the Select key supports the operation of attaching a single Arbitrary object to a key. You can attach an object by using the Attach method, and then get the object through the attachment method.
Multiple concurrent threads can safely use the Select key. In general, the read and write operations of the interest collection are synchronized with certain actions of the selector. How to perform this synchronization is related to implementation: in a generic implementation, if a selection operation is in progress, reading or writing the interest collection may block indefinitely, and in high-performance implementations, it may only be temporarily blocked. In either case, the select operation will always use the current interest collection value at the start of the operation.
-
Start from the following versions:
-
1.4
-
See also:
-
Selectablechannel, Selector
Field Summary |
static int |
OP_ACCEPT The operation set bit used for the socket accept operation. |
static int |
OP_CONNECT The operation set bit used for the socket connection operation. |
static int |
OP_READ The operation set bit used for the read operation. |
static int |
OP_WRITE The operation set bit for the write operation. |
Construction Method Summary |
protected |
SelectionKey() Constructs an instance of this class. |
Method Summary |
Object |
attach(Object ob) Attaches the given object to this key. |
Object |
attachment() Gets the current attached object. |
abstract void |
cancel() Requests that the channel to cancel this key be registered with its selector. |
abstract SelectableChannel |
channel() Returns the channel for which this key was created. |
abstract int |
interestOps() Gets the interest collection for this key. |
abstract SelectionKey |
interestOps(int ops) Sets the interest collection of this key to the given value. |
boolean |
isAcceptable() Tests whether the channel for this key is ready to accept a new socket connection. |
boolean |
isConnectable() Tests whether the channel for this key has completed its socket connection operation. |
boolean |
isReadable() Tests whether the channel for this key is ready for reading. |
abstract boolean |
isValid() Tells if the key is valid. |
boolean |
isWritable() Tests whether the channel for this key is ready for writing. |
abstract int |
readyOps() Gets the set of ready operations for this key. |
abstract Selector |
selector() Returns the key created for this selector. |
Methods of inheriting from class Java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Op_read
Op_read
-
The operation set bit used for the read operation.
Assume that the interest collection of the Select key already contains Op_readwhen the selection operation starts. If the selector detects that the appropriate channel is ready for reading, has reached the end of the stream, has been remotely shut down for further reading, or has a pending error, it adds Op_readto the prepared collection of the key and adds the key to the selected key set.
-
See also:
-
constant field value
Op_write
Op_write
-
The operation set bit for the write operation.
Assume that the interest collection of the Select key already contains Op_writewhen the selection operation starts. If the selector detects that the appropriate channel is ready for writing, has been shut down remotely and cannot be further written, or has a pending error, it adds op_writeto the set of the ready collection for that key and adds the key to the selected key set.
-
See also:
-
constant field value
Op_connect
Op_connect
-
The operation set bit used for the socket connection operation.
Assume that the interest collection of the Select key already contains Op_connectwhen the selection operation starts. If the selector detects that the appropriate socket channel is ready to complete its connection sequence, or has a pending error, it adds Op_connectto the Ready collection of the key and adds the key to the selected key set.
-
See also:
-
constant field value
Op_accept
Op_accept
-
The operation set bit used for the socket accept operation.
Assume that the interest collection of the Select key already contains Op_acceptwhen the selection operation starts. If the selector detects that the appropriate server socket channel is ready to accept another connection, or has a pending error, it adds op_acceptto the set of ready collection for that key and adds the key to the selected key set.
-
See also:
-
constant field value
Construction Method Details |
Selectionkey
Selectionkey ()
-
constructs an instance of this class.
Channel
Channel ()
-
returns the channel for which this key was created. This method continues to return a channel even if the key has been canceled.
-
-
Return:
-
the channel for this key
Selector
selector ()
-
returns the key created for this selector. This method continues to return the selector even after the key has been canceled.
-
-
Return:
-
selector for this key
IsValid
IsValid ()
-
Tells if the key is valid.
The key is valid when it is created and remains valid until it is canceled, its channel is closed, or its selector is closed.
-
-
Return:
-
returns true if and only if this key is valid
Cancel
Cancel ()
Requests that the
-
channel to cancel this key be registered with its selector. Once returned, the key is invalid and will be added to its selector's set of canceled keys. When you make the next selection operation, the key is removed from the key set for all selectors.
If this key has been canceled, calling this method is not valid. Once a key is canceled, the key remains invalid.
This method can be called at any time. This method is synchronized with the deselected keyset of the selector, so it may be temporarily blocked if it is called concurrently by a cancel or select operation involving the same selector.
Interestops
Interestops ()
-
Gets the interest collection for this key.
The returned collection is guaranteed to contain only the operation bits that are valid for the channel for this key.
This method can be called at any time. Whether it is blocked, and how long it will be blocked, is implementation-related.
-
-
Return:
-
The interest collection for this key
-
Thrown:
-
CancelledKeyException
-If this key is canceled
Interestops
Interestops (int ops)
-
Sets the interest collection of this key to the given value.
This method can be called at any time. Whether it is blocked, and how long it will be blocked, is implementation-related.
-
-
Parameters:
-
ops
-New collection of interest
-
Return:
-
This selection key
-
Thrown:
-
IllegalArgumentException
-If a bit in the collection does not correspond to an operation supported by the channel of this key, that is, if set & ~ (Channel (). Validops ())! = 0
-
CancelledKeyException
-If this key is canceled
Readyops
Readyops ()
-
Gets the set of ready operations for this key.
The returned collection is guaranteed to contain only the operation bits that are valid for the channel for this key.
-
-
Return:
-
Set of ready operations for this key
-
Thrown:
-
CancelledKeyException
-If this key is canceled
IsReadable
isreadable ()
-
tests whether the channel for this key is ready for reading.
The call to this method is k.isreadable () , which is exactly the same as the following call:
K.readyops () & Op_read! = 0
If the channel for this key does not support read operations, this method always returns false.
-
-
Return:
-
Returns true when and only if readyops () & op_read are not 0 values
-
Thrown:
-
CancelledKeyException
-If this key is canceled
IsWritable
iswritable ()
-
Tests whether the channel for this key is ready for writing.
The call to this method is k.iswritable () , which is exactly the same as the following call:
K.readyops () & Op_write! = 0
If the channel for this key does not support write operations, this method always returns false.
-
-
Return:
-
Returns true when and only if readyops () & op_write are not 0 values
-
Thrown:
-
CancelledKeyException
-If this key is canceled
Isconnectable
isconnectable ()
-
Tests whether the channel for this key has completed its socket connection operation.
The call to this method is k.isconnectable () , which is exactly the same as the following call:
K.readyops () & Op_connect! = 0
This method always returns falseif the channel for this key does not support socket connection operations.
-
-
Return:
-
Returns true when and only if readyops () & op_connect are not 0 values
-
Thrown:
-
CancelledKeyException
-If this key is canceled
Isacceptable
isacceptable ()
-
Tests whether the channel for this key is ready to accept a new socket connection.
The call to this method is in the form of k.isacceptable () , which is exactly the same as the following call:
K.readyops () & op_accept! = 0
If the channel for this key does not support socket accept operations, this method always returns false.
-
-
Return:
-
Returns true when and only if readyops () & op_accept are not 0 values
-
Thrown:
-
CancelledKeyException
-If this key is canceled
Attach
Attach (Object ob)
-
Attaches the given object to this key.
The attached object can then be obtained through the attachment method. Only one object can be attached at a time; Calling this method causes all previous attached objects to be discarded. The current additional object can be discarded by appending null .
-
-
Parameters:
-
ob
-Object to attach, can be null
-
Return:
-
objects that were previously attached (if any), otherwise null is returned
Attachment
Attachment ()
-
Gets the current attached object.
-
-
Return:
-
an object that is currently attached to this key, or null if there are no additional objects
Selectionkey Similar usage
- How to use Selectionkey and examples under Android
Selectionkey API Usage