APIdia Launch Partner
Top Description Fields Constructors Methods
java.nio.channels.spi

public abstract Class AbstractSelector

extends Selector
Class Inheritance
Known Direct Subclasses
sun.nio.ch.SelectorImpl
Imports
java.io.IOException, java.lang.invoke.MethodHandles, .VarHandle, java.nio.channels.SelectionKey, .Selector, java.util.HashSet, .Set, jdk.internal.invoke.MhUtil, sun.nio.ch.Interruptible, .SelectorImpl

Base implementation class for selectors.

This class encapsulates the low-level machinery required to implement the interruption of selection operations. A concrete selector class must invoke the begin and end methods before and after, respectively, invoking an I/O operation that might block indefinitely. In order to ensure that the end method is always invoked, these methods should be used within a try ... finally block:

try { begin(); // Perform blocking I/O operation here ... } finally { end(); }
try {
    begin();
    // Perform blocking I/O operation here
    ...
} finally {
    end();
}

This class also defines methods for maintaining a selector's cancelled-key set and for removing a key from its channel's key set, and declares the abstract register method that is invoked by a selectable channel's register method in order to perform the actual work of registering a channel.

Authors
Mark Reinhold, JSR-51 Expert Group
Since
1.4

Field Summary

Modifier and TypeField and Description
private final Set<SelectionKey>
private static final VarHandle
private volatile boolean
private final Interruptible
private final SelectorProvider

Constructor Summary

AccessConstructor and Description
protected
AbstractSelector(SelectorProvider
The provider that created this selector
provider
)

Initializes a new instance of this class.

Method Summary

Modifier and TypeMethod and Description
protected final void
begin()

Marks the beginning of an I/O operation that might block indefinitely.

pack-priv void
protected final Set<SelectionKey>

Returns:

The cancelled-key set
cancelledKeys
()

Retrieves this selector's cancelled-key set.

public final void
close()

Implements abstract java.nio.channels.Selector.close.

Implements java.io.Closeable.close.

Closes this selector.

protected final void
deregister(AbstractSelectionKey
The selection key to be removed
key
)

Removes the given key from its channel's key set.

protected final void
end()

Marks the end of an I/O operation that might block indefinitely.

protected abstract void
implCloseSelector()

Closes this selector.

public final boolean
isOpen()

Implements abstract java.nio.channels.Selector.isOpen.

Tells whether or not this selector is open.

public final SelectorProvider

Returns:

The provider that created this channel
provider
()

Implements abstract java.nio.channels.Selector.provider.

Returns the provider that created this channel.

protected abstract SelectionKey

Returns:

A new key representing the registration of the given channel with this selector
register
(AbstractSelectableChannel
The channel to be registered
ch
,
int
The initial interest set, which must be valid
ops
,
Object
The initial attachment for the resulting key
att
)

Registers the given channel with this selector.

Inherited from java.nio.channels.Selector:
keysopenselectselectselectselectselectedKeysselectNowselectNowwakeup

Field Detail

cancelledKeysback to summary
private final Set<SelectionKey> cancelledKeys
CLOSEDback to summary
private static final VarHandle CLOSED
closedback to summary
private volatile boolean closed
interruptorback to summary
private final Interruptible interruptor
providerback to summary
private final SelectorProvider provider

Constructor Detail

AbstractSelectorback to summary
protected AbstractSelector(SelectorProvider provider)

Initializes a new instance of this class.

Parameters
provider:SelectorProvider

The provider that created this selector

Method Detail

beginback to summary
protected final void begin()

Marks the beginning of an I/O operation that might block indefinitely.

This method should be invoked in tandem with the end method, using a try ... finally block as shown above, in order to implement interruption for this selector.

Invoking this method arranges for the selector's wakeup method to be invoked if a thread's interrupt method is invoked while the thread is blocked in an I/O operation upon the selector.

cancelback to summary
pack-priv void cancel(SelectionKey k)
cancelledKeysback to summary
protected final Set<SelectionKey> cancelledKeys()

Retrieves this selector's cancelled-key set.

This set should only be used while synchronized upon it.

Returns:Set<SelectionKey>

The cancelled-key set

closeback to summary
public final void close() throws IOException

Implements abstract java.nio.channels.Selector.close.

Implements java.io.Closeable.close.

Closes this selector.

If the selector has already been closed then this method returns immediately. Otherwise it marks the selector as closed and then invokes the implCloseSelector method in order to complete the close operation.

Exceptions
IOException:
If an I/O error occurs
deregisterback to summary
protected final void deregister(AbstractSelectionKey key)

Removes the given key from its channel's key set.

This method must be invoked by the selector for each channel that it deregisters.

Parameters
key:AbstractSelectionKey

The selection key to be removed

endback to summary
protected final void end()

Marks the end of an I/O operation that might block indefinitely.

This method should be invoked in tandem with the begin method, using a try ... finally block as shown above, in order to implement interruption for this selector.

implCloseSelectorback to summary
protected abstract void implCloseSelector() throws IOException

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.

Exceptions
IOException:
If an I/O error occurs while closing the selector
isOpenback to summary
public final boolean isOpen()

Implements abstract java.nio.channels.Selector.isOpen.

Doc from java.nio.channels.Selector.isOpen.

Tells whether or not this selector is open.

providerback to summary
public final SelectorProvider provider()

Implements abstract java.nio.channels.Selector.provider.

Returns the provider that created this channel.

Returns:SelectorProvider

The provider that created this channel

registerback to summary
protected abstract SelectionKey register(AbstractSelectableChannel ch, int ops, Object att)

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.

Parameters
ch:AbstractSelectableChannel

The channel to be registered

ops:int

The initial interest set, which must be valid

att:Object

The initial attachment for the resulting key

Returns:SelectionKey

A new key representing the registration of the given channel with this selector