Class Selector
- java.lang.Object
-
- java.nio.channels.Selector
-
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
AbstractSelector
public abstract class Selector extends Object implements Closeable
A controller for the selection ofSelectableChannelobjects. Selectable channels can be registered with a selector and get aSelectionKeythat represents the registration. The keys are also added to the selector's key set. Selection keys can be canceled so that the corresponding channel is no longer registered with the selector.By invoking the
selectmethod, the key set is checked and all keys that have been canceled since last select operation are moved to the set of canceled keys. During the select operation, the channels registered with this selector are checked to see whether they are ready for operation according to theirinterest set.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedSelector()Constructs a newSelector.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidclose()Closes this selector.abstract booleanisOpen()Indicates whether this selector is open.abstract Set<SelectionKey>keys()Gets the set of registered keys.static Selectoropen()Returns a selector returned bySelectorProvider.provider'sSelectorProvider.openSelector()method.abstract SelectorProviderprovider()Gets the provider of this selector.abstract intselect()Detects if any of the registered channels is ready for I/O operations according to itsinterest set.abstract intselect(long timeout)Detects if any of the registered channels is ready for I/O operations according to itsinterest set.abstract Set<SelectionKey>selectedKeys()Gets the selection keys whose channels are ready for operation.abstract intselectNow()Detects if any of the registered channels is ready for I/O operations according to itsinterest set.abstract Selectorwakeup()Forces blockedselectoperations to return immediately.
-
-
-
Method Detail
-
open
public static Selector open() throws IOException
Returns a selector returned bySelectorProvider.provider'sSelectorProvider.openSelector()method.- Throws:
IOException- if an I/O error occurs.
-
close
public abstract void close() throws IOExceptionCloses this selector. Ongoing calls to theselectmethods of this selector will get interrupted. This interruption behaves as if thewakeup()method of this selector is called. After this, all keys that are still valid are invalidated and their channels are unregistered. All resources held by this selector are released.Any further attempt of using this selector after this method has been called (except calling
close()orwakeup()) results in aClosedSelectorExceptionbeing thrown.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs.
-
isOpen
public abstract boolean isOpen()
Indicates whether this selector is open.- Returns:
trueif this selector is not closed,falseotherwise.
-
keys
public abstract Set<SelectionKey> keys()
Gets the set of registered keys.The returned set cannot be changed directly but can be modified indirectly by operations on the Selector. It should therefore not be treated as thread-safe.
- Returns:
- the set of registered keys.
-
provider
public abstract SelectorProvider provider()
Gets the provider of this selector.- Returns:
- the provider of this selector.
-
select
public abstract int select() throws IOExceptionDetects if any of the registered channels is ready for I/O operations according to itsinterest set. This method does not return until at least one channel is ready,wakeup()is invoked or the calling thread is interrupted.- Returns:
- the number of channels that are ready for operation.
- Throws:
IOException- if an I/O error occurs.ClosedSelectorException- if the selector is closed.
-
select
public abstract int select(long timeout) throws IOExceptionDetects if any of the registered channels is ready for I/O operations according to itsinterest set. This method does not return until at least one channel is ready,wakeup()is invoked, the calling thread is interrupted or the specifiedtimeoutexpires.- Parameters:
timeout- the non-negative timeout in millisecond; 0 will block forever if no channels get ready.- Returns:
- the number of channels that are ready for operation.
- Throws:
ClosedSelectorException- if the selector is closed.IllegalArgumentException- if the given timeout argument is less than zero.IOException- if an I/O error occurs.
-
selectedKeys
public abstract Set<SelectionKey> selectedKeys()
Gets the selection keys whose channels are ready for operation.Keys cannot be added to the set directly. Keys can be removed. The set can be modified indirectly by operations on the Selector. It should therefore not be treated as thread-safe.
- Returns:
- the selection keys whose channels are ready for operation.
- Throws:
ClosedSelectorException- if the selector is closed.
-
selectNow
public abstract int selectNow() throws IOExceptionDetects if any of the registered channels is ready for I/O operations according to itsinterest set. This operation will return immediately.- Returns:
- the number of channels that are ready for operation, 0 if none is ready.
- Throws:
IOException- if an I/O error occurrs.ClosedSelectorException- if the selector is closed.
-
wakeup
public abstract Selector wakeup()
Forces blockedselectoperations to return immediately.If no
selectoperation is blocked whenwakeup()is called then the nextselectoperation will return immediately. This can be undone by a call toselectNow(); after callingselectNow(), a subsequent call ofselectcan block again.- Returns:
- this selector.
- Throws:
ClosedSelectorException- if the selector is closed.
-
-