FileDocCategorySizeDatePackage
Selector.javaAPI DocAndroid 1.5 API6892Wed May 06 22:41:04 BST 2009java.nio.channels

Selector

public abstract class Selector extends Object
A controller for the selection of {@link SelectableChannel} objects. Selectable channels can be registered with a selector and get a {@link 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 {@code 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 {@link SelectionKey interest set}.

since
Android 1.0

Fields Summary
Constructors Summary
protected Selector()
Constructs a new {@code Selector}.

since
Android 1.0

        super();
    
Methods Summary
public abstract voidclose()
Closes this selector. Ongoing calls to the {@code select} methods of this selector will get interrupted. This interruption behaves as if the {@link #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 {@link #close()} or {@link #wakeup()}) results in a {@link ClosedSelectorException} being thrown.

throws
IOException if an I/O error occurs.
since
Android 1.0

public abstract booleanisOpen()
Indicates whether this selector is open.

return
{@code true} if this selector is not closed, {@code false} otherwise.
since
Android 1.0

public abstract java.util.Setkeys()
Gets the set of registered keys. The set is immutable and is not thread- safe.

return
the set of registered keys.
since
Android 1.0

public static java.nio.channels.Selectoropen()
The factory method for selector. It returns the selector returned by the default {@link SelectorProvider} by calling its {@code openCollector} method.

return
a new selector.
throws
IOException if an I/O error occurs.
since
Android 1.0

        return SelectorProvider.provider().openSelector();
    
public abstract java.nio.channels.spi.SelectorProviderprovider()
Gets the provider of this selector.

return
the provider of this selector.
since
Android 1.0

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

return
the number of channels that are ready for operation.
throws
IOException if an I/O error occurs.
throws
ClosedSelectorException if the selector is closed.
since
Android 1.0

public abstract intselect(long timeout)
Detects if any of the registered channels is ready for I/O operations according to its {@link SelectionKey interest set}. This method does not return until at least one channel is ready, {@link #wakeup()} is invoked, the calling thread is interrupted or the specified {@code timeout} expires.

param
timeout the non-negative timeout in millisecond; 0 will block forever if no channels get ready.
return
the number of channels that are ready for operation.
throws
ClosedSelectorException if the selector is closed.
throws
IllegalArgumentException if the given timeout argument is less than zero.
throws
IOException if an I/O error occurs.
since
Android 1.0

public abstract intselectNow()
Detects if any of the registered channels is ready for I/O operations according to its {@link SelectionKey interest set}. This operation will return immediately.

return
the number of channels that are ready for operation, 0 if none is ready.
throws
IOException if an I/O error occurrs.
throws
ClosedSelectorException if the selector is closed.
since
Android 1.0

public abstract java.util.SetselectedKeys()
Gets the selection keys whose channels are ready for operation. The set is not thread-safe and no keys may be added to it. Removing keys is allowed.

return
the selection keys whose channels are ready for operation.
throws
ClosedSelectorException if the selector is closed.
since
Android 1.0

public abstract java.nio.channels.Selectorwakeup()
Forces blocked {@code select} operations to return immediately.

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

return
this selector.
throws
ClosedSelectorException if the selector is closed.
since
Android 1.0