Most visited

Recently visited

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


可以通过 Selector进行复用的 Selector

为了与选择器一起使用,必须先通过register方法注册此类的实例。 此方法返回一个新的SelectionKey对象,该对象表示通道注册到选择器。

一旦注册了选择器,频道将保持注册状态,直到注销为止。 这涉及重新分配由选择器分配给信道的任何资源。

一个频道不能直接注销; 相反,代表其注册的密钥必须取消 取消密钥请求在选择器的下一次选择操作期间注销该通道。 通过调用其cancel方法可以明确取消密钥。 当通道关闭时,通过调用其close方法或通过中断通道上的I / O操作中阻塞的线程,所有通道的键都会被隐式取消。

如果选择器本身已关闭,则通道将被取消注册,并且代表其注册的密钥将被无效,而不再延迟。

一个频道最多可以注册一次,并且使用任何特定的选择器。

通过调用 isRegistered方法可以确定是否向一个或多个选择器注册频道。

可选通道可安全地用于多个并发线程。

Blocking mode

A selectable channel is either in blocking mode or in non-blocking mode. In blocking mode, every I/O operation invoked upon the channel will block until it completes. In non-blocking mode an I/O operation will never block and may transfer fewer bytes than were requested or possibly no bytes at all. The blocking mode of a selectable channel may be determined by invoking its
isBlocking method.

新创建的可选频道始终处于阻止模式。 非阻塞模式与基于选择器的多路复用相结合最为有用。 频道必须在注册到选择器之前进入非阻止模式,并且在注销之前可能不会返回到阻止模式。

也可以看看:

Summary

Protected constructors

SelectableChannel()

初始化此类的新实例。

Public methods

abstract Object blockingLock()

检索 configureBlockingregister方法同步的对象。

abstract SelectableChannel configureBlocking(boolean block)

调整此频道的屏蔽模式。

abstract boolean isBlocking()

告诉该通道上的每个I / O操作是否会阻塞,直到完成。

abstract boolean isRegistered()

确定此频道是否正在使用任何选择器进行注册。

abstract SelectionKey keyFor(Selector sel)

用给定的选择器检索表示通道注册的键。

abstract SelectorProvider provider()

返回创建此频道的提供商。

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

用给定的选择器注册此通道,返回选择键。

final SelectionKey register(Selector sel, int ops)

用给定的选择器注册此通道,返回选择键。

abstract int validOps()

返回识别此频道支持的操作的 operation set

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 ()

初始化此类的新实例。

Public methods

blockingLock

Added in API level 1
Object blockingLock ()

检索configureBlockingregister方法同步的对象。 这在实现需要短时间维护特定阻塞模式的适配器时非常有用。

Returns
Object The blocking-mode lock object

configureBlocking

Added in API level 1
SelectableChannel configureBlocking (boolean block)

调整此频道的屏蔽模式。

如果此通道注册了一个或多个选择器,则尝试将其置于阻塞模式将导致 IllegalBlockingModeException被抛出。

这个方法可以在任何时候调用。 新的阻塞模式只会影响此方法返回后启动的I / O操作。 对于某些实现,这可能需要阻塞,直到所有挂起的I / O操作完成。

如果在另一方法或 register方法的调用正在进行时调用此方法,则它将首先阻塞,直到其他操作完成。

Parameters
block boolean: If true then this channel will be placed in blocking mode; if false then it will be placed non-blocking mode
Returns
SelectableChannel This selectable channel
Throws
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

Added in API level 1
boolean isBlocking ()

告诉该通道上的每个I / O操作是否会阻塞,直到完成。 新创建的频道始终处于阻止模式。

如果此通道关闭,则此方法返回的值未指定。

Returns
boolean true if, and only if, this channel is in blocking mode

isRegistered

Added in API level 1
boolean isRegistered ()

确定此频道是否正在使用任何选择器进行注册。 新创建的频道未注册。

由于密钥取消和频道注销之间的固有延迟,一个频道在其所有密钥被取消后可能会保留一段时间的注册。 频道在关闭后也可能会保留一段时间。

Returns
boolean true if, and only if, this channel is registered

keyFor

Added in API level 1
SelectionKey keyFor (Selector sel)

用给定的选择器检索表示通道注册的键。

Parameters
sel Selector
Returns
SelectionKey The key returned when this channel was last registered with the given selector, or null if this channel is not currently registered with that selector

provider

Added in API level 1
SelectorProvider provider ()

返回创建此频道的提供商。

Returns
SelectorProvider The provider that created this channel

register

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

用给定的选择器注册此通道,返回选择键。

如果这个频道当前在给定的选择器中注册,则表示该注册的选择键被返回。 密钥的兴趣集将被更改为ops ,就像调用interestOps(int)方法一样。 如果参数att不是null,则该键的附件将被设置为该值。 如果密钥已被取消,则会引发CancelledKeyException

否则,这个通道还没有被注册到给定的选择器,所以它被注册并返回结果的新密钥。 密钥的初始兴趣集为ops ,其附件为att

这个方法可以在任何时候调用。 如果在调用此方法或configureBlocking此方法的另一调用时调用此方法,则它将首先阻塞,直到其他操作完成。 然后该方法将在选择器的密钥集上进行同步,因此如果与涉及相同选择器的另一注册或选择操作同时被调用,则该方法可能被阻塞。

如果此通道在此操作正在进行时关闭,则此方法返回的密钥将被取消,因此将无效。

Parameters
sel Selector: The selector with which this channel is to be registered
ops int: The interest set for the resulting key
att Object: The attachment for the resulting key; may be null
Returns
SelectionKey A key representing the registration of this channel with the given selector
Throws
ClosedChannelException If this channel is closed
ClosedSelectorException If the selector is closed
IllegalBlockingModeException If this channel is in blocking mode
IllegalSelectorException If this channel was not created by the same provider as the given selector
CancelledKeyException If this channel is currently registered with the given selector but the corresponding key has already been cancelled
IllegalArgumentException If a bit in the ops set does not correspond to an operation that is supported by this channel, that is, if set & ~validOps() != 0

register

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

用给定的选择器注册此通道,返回选择键。

这种方便的表单调用

sc.register(sel, ops)
behaves in exactly the same way as the invocation
sc.register(sel, ops, null)

Parameters
sel Selector: The selector with which this channel is to be registered
ops int: The interest set for the resulting key
Returns
SelectionKey A key representing the registration of this channel with the given selector
Throws
ClosedChannelException If this channel is closed
ClosedSelectorException If the selector is closed
IllegalBlockingModeException If this channel is in blocking mode
IllegalSelectorException If this channel was not created by the same provider as the given selector
CancelledKeyException If this channel is currently registered with the given selector but the corresponding key has already been cancelled
IllegalArgumentException If a bit in ops does not correspond to an operation that is supported by this channel, that is, if set & ~validOps() != 0

validOps

Added in API level 1
int validOps ()

返回标识此频道支持的操作的operation set 在此整数值中设置的位正好表示对此通道有效的操作。 此方法始终为给定的具体渠道类返回相同的值。

Returns
int The valid-operation set

Hooray!