- java.lang.Object
-
- java.nio.channels.Selector
-
- java.nio.channels.spi.AbstractSelector
-
- All Implemented Interfaces:
- Closeable, AutoCloseable
public abstract class AbstractSelector extends Selector
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
beginandendmethods before and after, respectively, invoking an I/O operation that might block indefinitely. In order to ensure that theendmethod is always invoked, these methods should be used within a try ... finally block: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
registermethod that is invoked by a selectable channel'sregistermethod in order to perform the actual work of registering a channel.- Since:
- 1.4
-
-
Constructor Summary
Constructors Modifier Constructor and Description protectedAbstractSelector(SelectorProvider provider)Initializes a new instance of this class.
-
Method Summary
Methods Modifier and Type Method and Description protected voidbegin()Marks the beginning of an I/O operation that might block indefinitely.protected Set<SelectionKey>cancelledKeys()Retrieves this selector's cancelled-key set.voidclose()Closes this selector.protected voidderegister(AbstractSelectionKey key)Removes the given key from its channel's key set.protected voidend()Marks the end of an I/O operation that might block indefinitely.protected abstract voidimplCloseSelector()Closes this selector.booleanisOpen()Tells whether or not this selector is open.SelectorProviderprovider()Returns the provider that created this channel.protected abstract SelectionKeyregister(AbstractSelectableChannel ch, int ops, Object att)Registers the given channel with this selector.
-
-
-
Constructor Detail
-
AbstractSelector
protected AbstractSelector(SelectorProvider provider)
Initializes a new instance of this class.
-
-
Method Detail
-
close
public final void close() throws IOExceptionCloses 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
implCloseSelectormethod in order to complete the close operation.- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceAutoCloseable- Specified by:
closein classSelector- Throws:
IOException- If an I/O error occurs
-
implCloseSelector
protected abstract void implCloseSelector() throws IOExceptionCloses this selector.This method is invoked by the
closemethod 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
wakeupmethod.- Throws:
IOException- If an I/O error occurs while closing the selector
-
isOpen
public final boolean isOpen()
Description copied from class:SelectorTells whether or not this selector is open.
-
provider
public final SelectorProvider provider()
Returns the provider that created this channel.
-
cancelledKeys
protected final Set<SelectionKey> cancelledKeys()
Retrieves this selector's cancelled-key set.This set should only be used while synchronized upon it.
- Returns:
- The cancelled-key set
-
register
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
registermethod in order to perform the actual work of registering the channel with this selector.- Parameters:
ch- The channel to be registeredops- The initial interest set, which must be validatt- The initial attachment for the resulting key- Returns:
- A new key representing the registration of the given channel with this selector
-
deregister
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- The selection key to be removed
-
begin
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
endmethod, using a try ... finally block as shown above, in order to implement interruption for this selector.Invoking this method arranges for the selector's
wakeupmethod to be invoked if a thread'sinterruptmethod is invoked while the thread is blocked in an I/O operation upon the selector.
-
end
protected final void end()
-
-
Document created the 11/06/2005, last modified the 04/03/2020
Source of the printed document:https://www.gaudry.be/en/java-api-rf-java/nio/channels/spi/AbstractSelector.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author of this site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.