Selector Class

Definition

A multiplexor of SelectableChannel objects.

[Android.Runtime.Register("java/nio/channels/Selector", DoNotGenerateAcw=true)]
public abstract class Selector : Java.Lang.Object, IDisposable, Java.Interop.IJavaPeerable, Java.IO.ICloseable
[<Android.Runtime.Register("java/nio/channels/Selector", DoNotGenerateAcw=true)>]
type Selector = class
    inherit Object
    interface ICloseable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Inheritance
Selector
Derived
Attributes
Implements

Remarks

A multiplexor of SelectableChannel objects.

A selector may be created by invoking the #open open method of this class, which will use the system's default java.nio.channels.spi.SelectorProvider selector provider to create a new selector. A selector may also be created by invoking the java.nio.channels.spi.SelectorProvider#openSelector openSelector method of a custom selector provider. A selector remains open until it is closed via its #close close method.

"ks">

A selectable channel's registration with a selector is represented by a SelectionKey object. A selector maintains three sets of selection keys:

<ul>

<li>

The key set contains the keys representing the current channel registrations of this selector. This set is returned by the #keys() keys method.

</li>

<li>

The selected-key set is the set of keys such that each key's channel was detected to be ready for at least one of the operations identified in the key's interest set during a prior selection operation that adds keys or updates keys in the set. This set is returned by the #selectedKeys() selectedKeys method. The selected-key set is always a subset of the key set.

</li>

<li>

The cancelled-key set is the set of keys that have been cancelled but whose channels have not yet been deregistered. This set is not directly accessible. The cancelled-key set is always a subset of the key set.

</li>

</ul>

All three sets are empty in a newly-created selector.

A key is added to a selector's key set as a side effect of registering a channel via the channel's SelectableChannel#register(Selector,int) register method. Cancelled keys are removed from the key set during selection operations. The key set itself is not directly modifiable.

A key is added to its selector's cancelled-key set when it is cancelled, whether by closing its channel or by invoking its SelectionKey#cancel cancel method. Cancelling a key will cause its channel to be deregistered during the next selection operation, at which time the key will be removed from all of the selector's key sets.

"sks">

Keys are added to the selected-key set by selection operations. A key may be removed directly from the selected-key set by invoking the set's java.util.Set#remove(java.lang.Object) remove method or by invoking the java.util.Iterator#remove() remove method of an java.util.Iterator iterator obtained from the set. All keys may be removed from the selected-key set by invoking the set's java.util.Set#clear() clear method. Keys may not be added directly to the selected-key set.

"selop"><h2>Selection</h2>

A selection operation queries the underlying operating system for an update as to the readiness of each registered channel to perform any of the operations identified by its key's interest set. There are two forms of selection operation:

<ol>

<li>

The #select(), #select(long), and #selectNow() methods add the keys of channels ready to perform an operation to the selected-key set, or update the ready-operation set of keys already in the selected-key set.

</li>

<li>

The #select(Consumer), #select(Consumer, long), and #selectNow(Consumer) methods perform an action on the key of each channel that is ready to perform an operation. These methods do not add to the selected-key set.

</li>

</ol>

<h3>Selection operations that add to the selected-key set</h3>

During each selection operation, keys may be added to and removed from a selector's selected-key set and may be removed from its key and cancelled-key sets. Selection is performed by the #select(), #select(long), and #selectNow() methods, and involves three steps:

<ol>

<li>

Each key in the cancelled-key set is removed from each key set of which it is a member, and its channel is deregistered. This step leaves the cancelled-key set empty.

</li>

<li>

The underlying operating system is queried for an update as to the readiness of each remaining channel to perform any of the operations identified by its key's interest set as of the moment that the selection operation began. For a channel that is ready for at least one such operation, one of the following two actions is performed:

<ol>

<li>

If the channel's key is not already in the selected-key set then it is added to that set and its ready-operation set is modified to identify exactly those operations for which the channel is now reported to be ready. Any readiness information previously recorded in the ready set is discarded.

</li>

<li>

Otherwise the channel's key is already in the selected-key set, so its ready-operation set is modified to identify any new operations for which the channel is reported to be ready. Any readiness information previously recorded in the ready set is preserved; in other words, the ready set returned by the underlying system is bitwise-disjoined into the key's current ready set.

</li>

</ol>

If all of the keys in the key set at the start of this step have empty interest sets then neither the selected-key set nor any of the keys' ready-operation sets will be updated.

<li>

If any keys were added to the cancelled-key set while step (2) was in progress then they are processed as in step (1).

</li>

</ol>

Whether or not a selection operation blocks to wait for one or more channels to become ready, and if so for how long, is the only essential difference between the three selection methods.

<h3>Selection operations that perform an action on selected keys</h3>

During each selection operation, keys may be removed from the selector's key, selected-key, and cancelled-key sets. Selection is performed by the #select(Consumer), #select(Consumer,long), and #selectNow(Consumer) methods, and involves three steps:

<ol>

<li>

Each key in the cancelled-key set is removed from each key set of which it is a member, and its channel is deregistered. This step leaves the cancelled-key set empty.

</li>

<li>

The underlying operating system is queried for an update as to the readiness of each remaining channel to perform any of the operations identified by its key's interest set as of the moment that the selection operation began.

For a channel that is ready for at least one such operation, the ready-operation set of the channel's key is set to identify exactly the operations for which the channel is ready and the action specified to the select method is invoked to consume the channel's key. Any readiness information previously recorded in the ready set is discarded prior to invoking the action.

Alternatively, where a channel is ready for more than one operation, the action may be invoked more than once with the channel's key and ready-operation set modified to a subset of the operations for which the channel is ready. Where the action is invoked more than once for the same key then its ready-operation set never contains operation bits that were contained in the set at previous calls to the action in the same selection operation.

</li>

<li>

If any keys were added to the cancelled-key set while step (2) was in progress then they are processed as in step (1).

</li>

</ol>

<h2>Concurrency</h2>

A Selector and its key set are safe for use by multiple concurrent threads. Its selected-key set and cancelled-key set, however, are not.

The selection operations synchronize on the selector itself, on the selected-key set, in that order. They also synchronize on the cancelled-key set during steps (1) and (3) above.

Changes made to the interest sets of a selector's keys while a selection operation is in progress have no effect upon that operation; they will be seen by the next selection operation.

Keys may be cancelled and channels may be closed at any time. Hence the presence of a key in one or more of a selector's key sets does not imply that the key is valid or that its channel is open. Application code should be careful to synchronize and check these conditions as necessary if there is any possibility that another thread will cancel a key or close a channel.

A thread blocked in a selection operation may be interrupted by some other thread in one of three ways:

<ul>

<li>

By invoking the selector's #wakeup wakeup method,

</li>

<li>

By invoking the selector's #close close method, or

</li>

<li>

By invoking the blocked thread's java.lang.Thread#interrupt() interrupt method, in which case its interrupt status will be set and the selector's #wakeup wakeup method will be invoked.

</li>

</ul>

The #close close method synchronizes on the selector and its selected-key set in the same order as in a selection operation.

"ksc">

A Selector's key set is safe for use by multiple concurrent threads. Retrieval operations from the key set do not generally block and so may overlap with new registrations that add to the set, or with the cancellation steps of selection operations that remove keys from the set. Iterators and spliterators return elements reflecting the state of the set at some point at or since the creation of the iterator/spliterator. They do not throw java.util.ConcurrentModificationException ConcurrentModificationException.

"sksc">

A selector's selected-key set is not, in general, safe for use by multiple concurrent threads. If such a thread might modify the set directly then access should be controlled by synchronizing on the set itself. The iterators returned by the set's java.util.Set#iterator() iterator methods are fail-fast: If the set is modified after the iterator is created, in any way except by invoking the iterator's own java.util.Iterator#remove() remove method, then a java.util.ConcurrentModificationException will be thrown.

Added in 1.4.

Java documentation for java.nio.channels.Selector.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Constructors

Selector()

Initializes a new instance of this class.

Selector(IntPtr, JniHandleOwnership)

A constructor used when creating managed representations of JNI objects; called by the runtime.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
IsOpen

Indicates whether this selector is open.

JniIdentityHashCode (Inherited from Object)
JniPeerMembers
PeerReference (Inherited from Object)
ThresholdClass

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

ThresholdType

This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Methods

Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Close()

Closes this selector.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
Equals(Object)

Indicates whether some other object is "equal to" this one.

(Inherited from Object)
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

(Inherited from Object)
Keys()

Returns this selector's key set.

Notify()

Wakes up a single thread that is waiting on this object's monitor.

(Inherited from Object)
NotifyAll()

Wakes up all threads that are waiting on this object's monitor.

(Inherited from Object)
Open()

Opens a selector.

Provider()

Returns the provider that created this channel.

Select()

Selects a set of keys whose corresponding channels are ready for I/O operations.

Select(IConsumer)

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

Select(IConsumer, Int64)

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

Select(Int64)

Selects a set of keys whose corresponding channels are ready for I/O operations.

SelectedKeys()

Returns this selector's selected-key set.

SelectNow()

Selects a set of keys whose corresponding channels are ready for I/O operations.

SelectNow(IConsumer)

Selects and performs an action on the keys whose corresponding channels are ready for I/O operations.

SetHandle(IntPtr, JniHandleOwnership)

Sets the Handle property.

(Inherited from Object)
ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Wait()

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>.

(Inherited from Object)
Wait(Int64)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wait(Int64, Int32)

Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed.

(Inherited from Object)
Wakeup()

Causes the first selection operation that has not yet returned to return immediately.

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to