AbstractSelectionKey
public abstract class SelectionKey extends Object
SelectableChannel
with a Selector
. A selection key is created each time a channel is registered with a selector. A key remains valid until it is cancelled by invoking its cancel
method, by closing its channel, or by closing its selector. Cancelling a key does not immediately remove it from its selector; it is instead added to the selector's cancelled-key set for removal during the next selection operation. The validity of a key may be tested by invoking its isValid
method.
A selection key contains two operation sets represented as integer values. Each bit of an operation set denotes a category of selectable operations that are supported by the key's channel.
The interest set determines which operation categories will be tested for readiness the next time one of the selector's selection methods is invoked. The interest set is initialized with the value given when the key is created; it may later be changed via the interestOps(int)
method.
The ready set identifies the operation categories for which the key's channel has been detected to be ready by the key's selector. The ready set is initialized to zero when the key is created; it may later be updated by the selector during a selection operation, but it cannot be updated directly.
That a selection key's ready set indicates that its channel is ready for some operation category is a hint, but not a guarantee, that an operation in such a category may be performed by a thread without causing the thread to block. A ready set is most likely to be accurate immediately after the completion of a selection operation. It is likely to be made inaccurate by external events and by I/O operations that are invoked upon the corresponding channel.
This class defines all known operation-set bits, but precisely which bits are supported by a given channel depends upon the type of the channel. Each subclass of SelectableChannel
defines a validOps()
method which returns a set identifying just those operations that are supported by the channel. An attempt to set or test an operation-set bit that is not supported by a key's channel will result in an appropriate run-time exception.
It is often necessary to associate some application-specific data with a selection key, for example an object that represents the state of a higher-level protocol and handles readiness notifications in order to implement that protocol. Selection keys therefore support the attachment of a single arbitrary object to a key. An object can be attached via the attach
method and then later retrieved via the attachment
method.
Selection keys are safe for use by multiple concurrent threads. A selection operation will always use the interest-set value that was current at the moment that the operation began.
Modifier and Type | Field | Description |
---|---|---|
static final int |
OP_ACCEPT |
Operation-set bit for socket-accept operations. |
static final int |
OP_CONNECT |
Operation-set bit for socket-connect operations. |
static final int |
OP_READ |
Operation-set bit for read operations. |
static final int |
OP_WRITE |
Operation-set bit for write operations. |
Modifier | Constructor | Description |
---|---|---|
protected |
Constructs an instance of this class. |
Modifier and Type | Method | Description |
---|---|---|
final Object |
attach |
Attaches the given object to this key. |
final Object |
attachment() |
Retrieves the current attachment. |
abstract void |
cancel() |
Requests that the registration of this key's channel with its selector be cancelled. |
abstract SelectableChannel |
channel() |
Returns the channel for which this key was created. |
abstract int |
interestOps() |
Retrieves this key's interest set. |
abstract SelectionKey |
interestOps |
Sets this key's interest set to the given value. |
int |
interestOpsAnd |
Atomically sets this key's interest set to the bitwise intersection ("and") of the existing interest set and the given value. |
int |
interestOpsOr |
Atomically sets this key's interest set to the bitwise union ("or") of the existing interest set and the given value. |
final boolean |
isAcceptable() |
Tests whether this key's channel is ready to accept a new socket connection. |
final boolean |
isConnectable() |
Tests whether this key's channel has either finished, or failed to finish, its socket-connection operation. |
final boolean |
isReadable() |
Tests whether this key's channel is ready for reading. |
abstract boolean |
isValid() |
Tells whether or not this key is valid. |
final boolean |
isWritable() |
Tests whether this key's channel is ready for writing. |
abstract int |
readyOps() |
Retrieves this key's ready-operation set. |
abstract Selector |
selector() |
Returns the selector for which this key was created. |
public static final int OP_READ
Suppose that a selection key's interest set contains OP_READ
at the start of a selection operation. If the selector detects that the corresponding channel is ready for reading, has reached end-of-stream, has been remotely shut down for further writing, or has an error pending, then it will add OP_READ
to the key's ready-operation set.
public static final int OP_WRITE
Suppose that a selection key's interest set contains OP_WRITE
at the start of a selection operation. If the selector detects that the corresponding channel is ready for writing, has been remotely shut down for further reading, or has an error pending, then it will add OP_WRITE
to the key's ready set.
public static final int OP_CONNECT
Suppose that a selection key's interest set contains OP_CONNECT
at the start of a selection operation. If the selector detects that the corresponding socket channel is ready to complete its connection sequence, or has an error pending, then it will add OP_CONNECT
to the key's ready set.
public static final int OP_ACCEPT
Suppose that a selection key's interest set contains OP_ACCEPT
at the start of a selection operation. If the selector detects that the corresponding server-socket channel is ready to accept another connection, or has an error pending, then it will add OP_ACCEPT
to the key's ready set.
protected SelectionKey()
public abstract SelectableChannel channel()
public abstract Selector selector()
public abstract boolean isValid()
A key is valid upon creation and remains so until it is cancelled, its channel is closed, or its selector is closed.
true
if, and only if, this key is validpublic abstract void cancel()
If this key has already been cancelled then invoking this method has no effect. Once cancelled, a key remains forever invalid.
This method may be invoked at any time. It synchronizes on the selector's cancelled-key set, and therefore may block briefly if invoked concurrently with a cancellation or selection operation involving the same selector.
public abstract int interestOps()
It is guaranteed that the returned set will only contain operation bits that are valid for this key's channel.
CancelledKeyException
- If this key has been cancelledpublic abstract SelectionKey interestOps(int ops)
This method may be invoked at any time. If this method is invoked while a selection operation is in progress then it has no effect upon that operation; the change to the key's interest set will be seen by the next selection operation.
ops
- The new interest setIllegalArgumentException
- If a bit in the set does not correspond to an operation that is supported by this key's channel, that is, if (ops & ~channel().validOps()) != 0
CancelledKeyException
- If this key has been cancelledpublic int interestOpsOr(int ops)
interestOpsAnd(int)
. This method may be invoked at any time. If this method is invoked while a selection operation is in progress then it has no effect upon that operation; the change to the key's interest set will be seen by the next selection operation.
interestOps()
and interestOps(int)
to retrieve and set this key's interest set.ops
- The interest set to applyIllegalArgumentException
- If a bit in the set does not correspond to an operation that is supported by this key's channel, that is, if (ops & ~channel().validOps()) != 0
CancelledKeyException
- If this key has been cancelledpublic int interestOpsAnd(int ops)
interestOpsOr(int)
. This method may be invoked at any time. If this method is invoked while a selection operation is in progress then it has no effect upon that operation; the change to the key's interest set will be seen by the next selection operation.
interestOps(int)
and interestOpsOr(int)
methods, this method does not throw IllegalArgumentException
when invoked with bits in the interest set that do not correspond to an operation that is supported by this key's channel. This is to allow operation bits in the interest set to be cleared using bitwise complement values, e.g., interestOpsAnd(~SelectionKey.OP_READ)
will remove the OP_READ
from the interest set without affecting other bits.interestOps()
and interestOps(int)
to retrieve and set this key's interest set.ops
- The interest set to applyCancelledKeyException
- If this key has been cancelledpublic abstract int readyOps()
It is guaranteed that the returned set will only contain operation bits that are valid for this key's channel.
CancelledKeyException
- If this key has been cancelledpublic final boolean isReadable()
An invocation of this method of the form k.isReadable()
behaves in exactly the same way as the expression
k.readyOps() & OP_READ != 0
If this key's channel does not support read operations then this method always returns false
.
true
if, and only if, readyOps() & OP_READ
is nonzeroCancelledKeyException
- If this key has been cancelledpublic final boolean isWritable()
An invocation of this method of the form k.isWritable()
behaves in exactly the same way as the expression
k.readyOps() & OP_WRITE != 0
If this key's channel does not support write operations then this method always returns false
.
true
if, and only if, readyOps() & OP_WRITE
is nonzeroCancelledKeyException
- If this key has been cancelledpublic final boolean isConnectable()
An invocation of this method of the form k.isConnectable()
behaves in exactly the same way as the expression
k.readyOps() & OP_CONNECT != 0
If this key's channel does not support socket-connect operations then this method always returns false
.
true
if, and only if, readyOps() & OP_CONNECT
is nonzeroCancelledKeyException
- If this key has been cancelledpublic final boolean isAcceptable()
An invocation of this method of the form k.isAcceptable()
behaves in exactly the same way as the expression
k.readyOps() & OP_ACCEPT != 0
If this key's channel does not support socket-accept operations then this method always returns false
.
true
if, and only if, readyOps() & OP_ACCEPT
is nonzeroCancelledKeyException
- If this key has been cancelledpublic final Object attach(Object ob)
An attached object may later be retrieved via the attachment
method. Only one object may be attached at a time; invoking this method causes any previous attachment to be discarded. The current attachment may be discarded by attaching null
.
ob
- The object to be attached; may be null
null
public final Object attachment()
null
if there is no attachment
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/channels/SelectionKey.html