Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Selector

public abstract class Selector
extends Object implements Closeable

java.lang.Object
   ↳ java.nio.channels.Selector
Known Direct Subclasses


A controller for the selection of SelectableChannel objects. Selectable channels can be registered with a selector and get a SelectionKey that 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 select method, 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 their interest set.

Summary

Protected constructors

Selector()

Constructs a new Selector.

Public methods

abstract void close()

Closes this selector.

abstract boolean isOpen()

Indicates whether this selector is open.

abstract Set<SelectionKey> keys()

Gets the set of registered keys.

static Selector open()

Returns a selector returned by provider()'s openSelector() method.

abstract SelectorProvider provider()

Gets the provider of this selector.

abstract int select()

Detects if any of the registered channels is ready for I/O operations according to its interest set.

abstract int select(long timeout)

Detects if any of the registered channels is ready for I/O operations according to its interest set.

abstract int selectNow()

Detects if any of the registered channels is ready for I/O operations according to its interest set.

abstract Set<SelectionKey> selectedKeys()

Gets the selection keys whose channels are ready for operation.

abstract Selector wakeup()

Forces blocked select operations to return immediately.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Protected constructors

Selector

Added in API level 1
Selector ()

Constructs a new Selector.

Public methods

close

Added in API level 1
void close ()

Closes this selector. Ongoing calls to the select methods of this selector will get interrupted. This interruption behaves as if the wakeup() 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() or wakeup()) results in a ClosedSelectorException being thrown.

Throws
IOException if an I/O error occurs.

isOpen

Added in API level 1
boolean isOpen ()

Indicates whether this selector is open.

Returns
boolean true if this selector is not closed, false otherwise.

keys

Added in API level 1
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
Set<SelectionKey> the set of registered keys.

open

Added in API level 1
Selector open ()

Returns a selector returned by provider()'s openSelector() method.

Returns
Selector
Throws
IOException if an I/O error occurs.

provider

Added in API level 1
SelectorProvider provider ()

Gets the provider of this selector.

Returns
SelectorProvider the provider of this selector.

select

Added in API level 1
int select ()

Detects if any of the registered channels is ready for I/O operations according to its interest set. This method does not return until at least one channel is ready, wakeup() is invoked or the calling thread is interrupted.

Returns
int the number of channels that are ready for operation.
Throws
IOException if an I/O error occurs.
ClosedSelectorException if the selector is closed.

select

Added in API level 1
int select (long timeout)

Detects if any of the registered channels is ready for I/O operations according to its interest set. This method does not return until at least one channel is ready, wakeup() is invoked, the calling thread is interrupted or the specified timeout expires.

Parameters
timeout long: the non-negative timeout in millisecond; 0 will block forever if no channels get ready.
Returns
int 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.

selectNow

Added in API level 1
int selectNow ()

Detects if any of the registered channels is ready for I/O operations according to its interest set. This operation will return immediately.

Returns
int 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.

selectedKeys

Added in API level 1
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
Set<SelectionKey> the selection keys whose channels are ready for operation.
Throws
ClosedSelectorException if the selector is closed.

wakeup

Added in API level 1
Selector wakeup ()

Forces blocked select operations to return immediately.

If no select operation is blocked when wakeup() is called then the next select operation will return immediately. This can be undone by a call to selectNow(); after calling selectNow(), a subsequent call of select can block again.

Returns
Selector this selector.
Throws
ClosedSelectorException if the selector is closed.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.