APIdia Launch Partner
Top Description Fields Constructors Methods
sun.nio.ch

public abstract Class SelectorImpl

extends AbstractSelector
Class Inheritance
Known Direct Subclasses
sun.nio.ch.EPollSelectorImpl, sun.nio.ch.PollSelectorImpl
Imports
java.io.IOException, java.nio.channels.CancelledKeyException, .ClosedSelectorException, .IllegalSelectorException, .SelectableChannel, .SelectionKey, java.nio.channels.spi.AbstractSelectableChannel, .AbstractSelector, .SelectorProvider, java.util.ArrayDeque, .Collections, .Deque, .HashSet, .Iterator, .Objects, .Set, java.util.concurrent.ConcurrentHashMap, java.util.function.Consumer

Base Selector implementation class.

Field Summary

Modifier and TypeField and Description
private final Deque<SelectionKeyImpl>
private boolean
private final Set<SelectionKey>
private final Set<SelectionKey>
private final Set<SelectionKey>
private final Set<SelectionKey>

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
protected final void
begin(boolean blocking)

Marks the beginning of a select operation that might block

public void
cancel(SelectionKeyImpl ski)

Queue a cancelled key for the next selection operation

protected abstract int
doSelect(Consumer<SelectionKey>
the action to perform, can be null
action
,
long
timeout in milliseconds to wait, 0 to not wait, -1 to wait indefinitely
timeout
)

Hides java.nio.channels.Selector.doSelect.

Selects the keys for channels that are ready for I/O operations.

protected final void
end(boolean blocking)

Marks the end of a select operation that may have blocked

private void
protected abstract void
implClose()

Invoked by implCloseSelector to close the selector.

public final void
protected abstract void
implDereg(SelectionKeyImpl ski)

Removes the key from the selector

protected void
implRegister(SelectionKeyImpl ski)

Register the key in the selector.

public final Set<SelectionKey>
keys()

Implements abstract java.nio.channels.Selector.keys.

Returns this selector's key set.

private int
lockAndDoSelect(Consumer<SelectionKey> action, long timeout)

protected final void
processDeregisterQueue()

Invoked by selection operations to process the cancelled keys

protected final int
processReadyEvents(int rOps, SelectionKeyImpl ski, Consumer<SelectionKey> action)

Invoked by selection operations to handle ready events.

protected final SelectionKey
register(AbstractSelectableChannel ch, int ops, Object attachment)

Implements abstract java.nio.channels.spi.AbstractSelector.register.

Registers the given channel with this selector.

public final int
select(long timeout)

Implements abstract java.nio.channels.Selector.select.

Selects a set of keys whose corresponding channels are ready for I/O operations.

public final int
select()

Implements abstract java.nio.channels.Selector.select.

Selects a set of keys whose corresponding channels are ready for I/O operations.

public final int
select(Consumer<SelectionKey> action, long timeout)

Overrides java.nio.channels.Selector.select.

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

public final int
select(Consumer<SelectionKey> action)

Overrides java.nio.channels.Selector.select.

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

public final Set<SelectionKey>
selectedKeys()

Implements abstract java.nio.channels.Selector.selectedKeys.

Returns this selector's selected-key set.

public final int
selectNow()

Implements abstract java.nio.channels.Selector.selectNow.

Selects a set of keys whose corresponding channels are ready for I/O operations.

public final int
selectNow(Consumer<SelectionKey> action)

Overrides java.nio.channels.Selector.selectNow.

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

protected abstract void
setEventOps(SelectionKeyImpl ski)

Invoked by interestOps to ensure the interest ops are updated at the next selection operation.

Inherited from java.nio.channels.spi.AbstractSelector:
begincancelledKeysclosederegisterendisOpenprovider

Field Detail

cancelledKeysback to summary
private final Deque<SelectionKeyImpl> cancelledKeys

Hides java.nio.channels.spi.AbstractSelector.cancelledKeys.

inSelectback to summary
private boolean inSelect
keysback to summary
private final Set<SelectionKey> keys
publicKeysback to summary
private final Set<SelectionKey> publicKeys
publicSelectedKeysback to summary
private final Set<SelectionKey> publicSelectedKeys
selectedKeysback to summary
private final Set<SelectionKey> selectedKeys

Constructor Detail

SelectorImplback to summary
protected SelectorImpl(SelectorProvider sp)

Method Detail

beginback to summary
protected final void begin(boolean blocking)

Marks the beginning of a select operation that might block

cancelback to summary
public void cancel(SelectionKeyImpl ski)

Queue a cancelled key for the next selection operation

doSelectback to summary
protected abstract int doSelect(Consumer<SelectionKey> action, long timeout) throws IOException

Hides java.nio.channels.Selector.doSelect.

Selects the keys for channels that are ready for I/O operations.

Parameters
action:Consumer<SelectionKey>

the action to perform, can be null

timeout:long

timeout in milliseconds to wait, 0 to not wait, -1 to wait indefinitely

endback to summary
protected final void end(boolean blocking)

Marks the end of a select operation that may have blocked

ensureOpenback to summary
private void ensureOpen()
implCloseback to summary
protected abstract void implClose() throws IOException

Invoked by implCloseSelector to close the selector.

implCloseSelectorback to summary
public final void implCloseSelector() throws IOException

Implements abstract java.nio.channels.spi.AbstractSelector.implCloseSelector.

Doc from java.nio.channels.spi.AbstractSelector.implCloseSelector.

Closes this selector.

This method is invoked by the close method in order to perform the actual work of closing the selector. This method is only invoked if the selector has not yet been closed, and it is never invoked more than once.

An implementation of this method must arrange for any other thread that is blocked in a selection operation upon this selector to return immediately as if by invoking the wakeup method.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs while closing the selector
implDeregback to summary
protected abstract void implDereg(SelectionKeyImpl ski) throws IOException

Removes the key from the selector

implRegisterback to summary
protected void implRegister(SelectionKeyImpl ski)

Register the key in the selector. The default implementation checks if the selector is open. It should be overridden by selector implementations as needed.

keysback to summary
public final Set<SelectionKey> keys()

Implements abstract java.nio.channels.Selector.keys.

Doc from java.nio.channels.Selector.keys.

Returns this selector's key set.

The key set is not directly modifiable. A key is removed only after it has been cancelled and its channel has been deregistered. Any attempt to modify the key set will cause an UnsupportedOperationException to be thrown.

The set is safe for use by multiple concurrent threads.

Annotations
@Override
lockAndDoSelectback to summary
private int lockAndDoSelect(Consumer<SelectionKey> action, long timeout) throws IOException
processDeregisterQueueback to summary
protected final void processDeregisterQueue() throws IOException

Invoked by selection operations to process the cancelled keys

processReadyEventsback to summary
protected final int processReadyEvents(int rOps, SelectionKeyImpl ski, Consumer<SelectionKey> action)

Invoked by selection operations to handle ready events. If an action is specified then it is invoked to handle the key, otherwise the key is added to the selected-key set (or updated when it is already in the set).

registerback to summary
protected final SelectionKey register(AbstractSelectableChannel ch, int ops, Object attachment)

Implements abstract java.nio.channels.spi.AbstractSelector.register.

Doc from java.nio.channels.spi.AbstractSelector.register.

Registers the given channel with this selector.

This method is invoked by a channel's register method in order to perform the actual work of registering the channel with this selector.

Annotations
@Override
selectback to summary
public final int select(long timeout) throws IOException

Implements abstract java.nio.channels.Selector.select.

Doc from java.nio.channels.Selector.select.

Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a blocking selection operation. It returns only after at least one channel is selected, this selector's wakeup method is invoked, the current thread is interrupted, or the given timeout period expires, whichever comes first.

This method does not offer real-time guarantees: It schedules the timeout as if by invoking the Object#wait(long) method.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
selectback to summary
public final int select() throws IOException

Implements abstract java.nio.channels.Selector.select.

Doc from java.nio.channels.Selector.select.

Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a blocking selection operation. It returns only after at least one channel is selected, this selector's wakeup method is invoked, or the current thread is interrupted, whichever comes first.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
selectback to summary
public final int select(Consumer<SelectionKey> action, long timeout) throws IOException

Overrides java.nio.channels.Selector.select.

Doc from java.nio.channels.Selector.select.

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

This method performs a blocking selection operation. It wakes up from querying the operating system only when at least one channel is selected, this selector's wakeup method is invoked, the current thread is interrupted, or the given timeout period expires, whichever comes first.

The specified action's accept method is invoked with the key for each channel that is ready to perform an operation identified by its key's interest set. The accept method may be invoked more than once for the same key but with the ready-operation set containing a subset of the operations for which the channel is ready (as described above). The accept method is invoked while synchronized on the selector and its selected-key set. Great care must be taken to avoid deadlocking with other threads that also synchronize on these objects. Selection operations are not reentrant in general and consequently the action should take great care not to attempt a selection operation on the same selector. The behavior when attempting a reentrant selection operation is implementation specific and therefore not specified. If the action closes the selector then ClosedSelectorException is thrown when the action completes. The action is not prohibited from closing channels registered with the selector, nor prohibited from cancelling keys or changing a key's interest set. If a channel is selected but its key is cancelled or its interest set changed before the action is performed on the key then it is implementation specific as to whether the action is invoked (it may be invoked with an invalid key). Exceptions thrown by the action are relayed to the caller.

This method does not offer real-time guarantees: It schedules the timeout as if by invoking the Object#wait(long) method.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
selectback to summary
public final int select(Consumer<SelectionKey> action) throws IOException

Overrides java.nio.channels.Selector.select.

Doc from java.nio.channels.Selector.select.

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

This method performs a blocking selection operation. It wakes up from querying the operating system only when at least one channel is selected, this selector's wakeup method is invoked, or the current thread is interrupted, whichever comes first.

This method is equivalent to invoking the 2-arg select method with a timeout of 0 to block indefinitely.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
selectedKeysback to summary
public final Set<SelectionKey> selectedKeys()

Implements abstract java.nio.channels.Selector.selectedKeys.

Doc from java.nio.channels.Selector.selectedKeys.

Returns this selector's selected-key set.

Keys may be removed from, but not directly added to, the selected-key set. Any attempt to add an object to the key set will cause an UnsupportedOperationException to be thrown.

The selected-key set is not thread-safe.

Annotations
@Override
selectNowback to summary
public final int selectNow() throws IOException

Implements abstract java.nio.channels.Selector.selectNow.

Doc from java.nio.channels.Selector.selectNow.

Selects a set of keys whose corresponding channels are ready for I/O operations.

This method performs a non-blocking selection operation. If no channels have become selectable since the previous selection operation then this method immediately returns zero.

Invoking this method clears the effect of any previous invocations of the wakeup method.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
selectNowback to summary
public final int selectNow(Consumer<SelectionKey> action) throws IOException

Overrides java.nio.channels.Selector.selectNow.

Doc from java.nio.channels.Selector.selectNow.

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

This method performs a non-blocking selection operation.

Invoking this method clears the effect of any previous invocations of the wakeup method.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
setEventOpsback to summary
protected abstract void setEventOps(SelectionKeyImpl ski)

Invoked by interestOps to ensure the interest ops are updated at the next selection operation.