0N/A/*
2362N/A * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.nio.channels;
0N/A
1956N/Aimport java.io.Closeable;
0N/Aimport java.io.IOException;
0N/Aimport java.nio.channels.spi.SelectorProvider;
0N/Aimport java.util.Set;
0N/A
0N/A
0N/A/**
0N/A * A multiplexor of {@link SelectableChannel} objects.
0N/A *
0N/A * <p> A selector may be created by invoking the {@link #open open} method of
0N/A * this class, which will use the system's default {@link
0N/A * java.nio.channels.spi.SelectorProvider </code>selector provider<code>} to
0N/A * create a new selector. A selector may also be created by invoking the
0N/A * {@link java.nio.channels.spi.SelectorProvider#openSelector openSelector}
0N/A * method of a custom selector provider. A selector remains open until it is
0N/A * closed via its {@link #close close} method.
0N/A *
0N/A * <a name="ks">
0N/A *
0N/A * <p> A selectable channel's registration with a selector is represented by a
0N/A * {@link SelectionKey} object. A selector maintains three sets of selection
0N/A * keys:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li><p> The <i>key set</i> contains the keys representing the current
0N/A * channel registrations of this selector. This set is returned by the
0N/A * {@link #keys() keys} method. </p></li>
0N/A *
0N/A * <li><p> The <i>selected-key set</i> is the set of keys such that each
0N/A * key's channel was detected to be ready for at least one of the operations
0N/A * identified in the key's interest set during a prior selection operation.
0N/A * This set is returned by the {@link #selectedKeys() selectedKeys} method.
0N/A * The selected-key set is always a subset of the key set. </p></li>
0N/A *
0N/A * <li><p> The <i>cancelled-key</i> set is the set of keys that have been
0N/A * cancelled but whose channels have not yet been deregistered. This set is
0N/A * not directly accessible. The cancelled-key set is always a subset of the
0N/A * key set. </p></li>
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p> All three sets are empty in a newly-created selector.
0N/A *
0N/A * <p> A key is added to a selector's key set as a side effect of registering a
0N/A * channel via the channel's {@link SelectableChannel#register(Selector,int)
0N/A * register} method. Cancelled keys are removed from the key set during
0N/A * selection operations. The key set itself is not directly modifiable.
0N/A *
0N/A * <p> A key is added to its selector's cancelled-key set when it is cancelled,
0N/A * whether by closing its channel or by invoking its {@link SelectionKey#cancel
0N/A * cancel} method. Cancelling a key will cause its channel to be deregistered
0N/A * during the next selection operation, at which time the key will removed from
0N/A * all of the selector's key sets.
0N/A *
0N/A * <a name="sks"><p> Keys are added to the selected-key set by selection
0N/A * operations. A key may be removed directly from the selected-key set by
0N/A * invoking the set's {@link java.util.Set#remove(java.lang.Object) remove}
0N/A * method or by invoking the {@link java.util.Iterator#remove() remove} method
0N/A * of an {@link java.util.Iterator </code>iterator<code>} obtained from the
0N/A * set. Keys are never removed from the selected-key set in any other way;
0N/A * they are not, in particular, removed as a side effect of selection
0N/A * operations. Keys may not be added directly to the selected-key set. </p>
0N/A *
0N/A *
0N/A * <a name="selop">
0N/A * <h4>Selection</h4>
0N/A *
0N/A * <p> During each selection operation, keys may be added to and removed from a
0N/A * selector's selected-key set and may be removed from its key and
0N/A * cancelled-key sets. Selection is performed by the {@link #select()}, {@link
0N/A * #select(long)}, and {@link #selectNow()} methods, and involves three steps:
0N/A * </p>
0N/A *
0N/A * <ol>
0N/A *
0N/A * <li><p> Each key in the cancelled-key set is removed from each key set of
0N/A * which it is a member, and its channel is deregistered. This step leaves
0N/A * the cancelled-key set empty. </p></li>
0N/A *
0N/A * <li><p> The underlying operating system is queried for an update as to the
0N/A * readiness of each remaining channel to perform any of the operations
0N/A * identified by its key's interest set as of the moment that the selection
0N/A * operation began. For a channel that is ready for at least one such
0N/A * operation, one of the following two actions is performed: </p>
0N/A *
0N/A * <ol type=a>
0N/A *
0N/A * <li><p> If the channel's key is not already in the selected-key set then
0N/A * it is added to that set and its ready-operation set is modified to
0N/A * identify exactly those operations for which the channel is now reported
0N/A * to be ready. Any readiness information previously recorded in the ready
0N/A * set is discarded. </p></li>
0N/A *
0N/A * <li><p> Otherwise the channel's key is already in the selected-key set,
0N/A * so its ready-operation set is modified to identify any new operations
0N/A * for which the channel is reported to be ready. Any readiness
0N/A * information previously recorded in the ready set is preserved; in other
0N/A * words, the ready set returned by the underlying system is
0N/A * bitwise-disjoined into the key's current ready set. </p></li>
0N/A *
0N/A * </ol></li>
0N/A *
0N/A * If all of the keys in the key set at the start of this step have empty
0N/A * interest sets then neither the selected-key set nor any of the keys'
0N/A * ready-operation sets will be updated.
0N/A *
0N/A * <li><p> If any keys were added to the cancelled-key set while step (2) was
0N/A * in progress then they are processed as in step (1). </p></li>
0N/A *
0N/A * </ol>
0N/A *
0N/A * <p> Whether or not a selection operation blocks to wait for one or more
0N/A * channels to become ready, and if so for how long, is the only essential
0N/A * difference between the three selection methods. </p>
0N/A *
0N/A *
0N/A * <h4>Concurrency</h4>
0N/A *
0N/A * <p> Selectors are themselves safe for use by multiple concurrent threads;
0N/A * their key sets, however, are not.
0N/A *
0N/A * <p> The selection operations synchronize on the selector itself, on the key
0N/A * set, and on the selected-key set, in that order. They also synchronize on
0N/A * the cancelled-key set during steps (1) and (3) above.
0N/A *
0N/A * <p> Changes made to the interest sets of a selector's keys while a
0N/A * selection operation is in progress have no effect upon that operation; they
0N/A * will be seen by the next selection operation.
0N/A *
0N/A * <p> Keys may be cancelled and channels may be closed at any time. Hence the
0N/A * presence of a key in one or more of a selector's key sets does not imply
0N/A * that the key is valid or that its channel is open. Application code should
0N/A * be careful to synchronize and check these conditions as necessary if there
0N/A * is any possibility that another thread will cancel a key or close a channel.
0N/A *
0N/A * <p> A thread blocked in one of the {@link #select()} or {@link
0N/A * #select(long)} methods may be interrupted by some other thread in one of
0N/A * three ways:
0N/A *
0N/A * <ul>
0N/A *
0N/A * <li><p> By invoking the selector's {@link #wakeup wakeup} method,
0N/A * </p></li>
0N/A *
0N/A * <li><p> By invoking the selector's {@link #close close} method, or
0N/A * </p></li>
0N/A *
0N/A * <li><p> By invoking the blocked thread's {@link
0N/A * java.lang.Thread#interrupt() interrupt} method, in which case its
0N/A * interrupt status will be set and the selector's {@link #wakeup wakeup}
0N/A * method will be invoked. </p></li>
0N/A *
0N/A * </ul>
0N/A *
0N/A * <p> The {@link #close close} method synchronizes on the selector and all
0N/A * three key sets in the same order as in a selection operation.
0N/A *
0N/A * <a name="ksc">
0N/A *
0N/A * <p> A selector's key and selected-key sets are not, in general, safe for use
0N/A * by multiple concurrent threads. If such a thread might modify one of these
0N/A * sets directly then access should be controlled by synchronizing on the set
0N/A * itself. The iterators returned by these sets' {@link
0N/A * java.util.Set#iterator() iterator} methods are <i>fail-fast:</i> If the set
0N/A * is modified after the iterator is created, in any way except by invoking the
0N/A * iterator's own {@link java.util.Iterator#remove() remove} method, then a
0N/A * {@link java.util.ConcurrentModificationException} will be thrown. </p>
0N/A *
0N/A *
0N/A * @author Mark Reinhold
0N/A * @author JSR-51 Expert Group
0N/A * @since 1.4
0N/A *
0N/A * @see SelectableChannel
0N/A * @see SelectionKey
0N/A */
0N/A
1956N/Apublic abstract class Selector implements Closeable {
0N/A
0N/A /**
0N/A * Initializes a new instance of this class.
0N/A */
0N/A protected Selector() { }
0N/A
0N/A /**
0N/A * Opens a selector.
0N/A *
0N/A * <p> The new selector is created by invoking the {@link
0N/A * java.nio.channels.spi.SelectorProvider#openSelector openSelector} method
0N/A * of the system-wide default {@link
0N/A * java.nio.channels.spi.SelectorProvider} object. </p>
0N/A *
0N/A * @return A new selector
0N/A *
0N/A * @throws IOException
0N/A * If an I/O error occurs
0N/A */
0N/A public static Selector open() throws IOException {
0N/A return SelectorProvider.provider().openSelector();
0N/A }
0N/A
0N/A /**
0N/A * Tells whether or not this selector is open. </p>
0N/A *
0N/A * @return <tt>true</tt> if, and only if, this selector is open
0N/A */
0N/A public abstract boolean isOpen();
0N/A
0N/A /**
0N/A * Returns the provider that created this channel. </p>
0N/A *
0N/A * @return The provider that created this channel
0N/A */
0N/A public abstract SelectorProvider provider();
0N/A
0N/A /**
0N/A * Returns this selector's key set.
0N/A *
0N/A * <p> The key set is not directly modifiable. A key is removed only after
0N/A * it has been cancelled and its channel has been deregistered. Any
0N/A * attempt to modify the key set will cause an {@link
0N/A * UnsupportedOperationException} to be thrown.
0N/A *
0N/A * <p> The key set is <a href="#ksc">not thread-safe</a>. </p>
0N/A *
0N/A * @return This selector's key set
0N/A *
0N/A * @throws ClosedSelectorException
0N/A * If this selector is closed
0N/A */
0N/A public abstract Set<SelectionKey> keys();
0N/A
0N/A /**
0N/A * Returns this selector's selected-key set.
0N/A *
0N/A * <p> Keys may be removed from, but not directly added to, the
0N/A * selected-key set. Any attempt to add an object to the key set will
0N/A * cause an {@link UnsupportedOperationException} to be thrown.
0N/A *
0N/A * <p> The selected-key set is <a href="#ksc">not thread-safe</a>. </p>
0N/A *
0N/A * @return This selector's selected-key set
0N/A *
0N/A * @throws ClosedSelectorException
0N/A * If this selector is closed
0N/A */
0N/A public abstract Set<SelectionKey> selectedKeys();
0N/A
0N/A /**
0N/A * Selects a set of keys whose corresponding channels are ready for I/O
0N/A * operations.
0N/A *
0N/A * <p> This method performs a non-blocking <a href="#selop">selection
0N/A * operation</a>. If no channels have become selectable since the previous
0N/A * selection operation then this method immediately returns zero.
0N/A *
0N/A * <p> Invoking this method clears the effect of any previous invocations
0N/A * of the {@link #wakeup wakeup} method. </p>
0N/A *
0N/A * @return The number of keys, possibly zero, whose ready-operation sets
0N/A * were updated by the selection operation
0N/A *
0N/A * @throws IOException
0N/A * If an I/O error occurs
0N/A *
0N/A * @throws ClosedSelectorException
0N/A * If this selector is closed
0N/A */
0N/A public abstract int selectNow() throws IOException;
0N/A
0N/A /**
0N/A * Selects a set of keys whose corresponding channels are ready for I/O
0N/A * operations.
0N/A *
0N/A * <p> This method performs a blocking <a href="#selop">selection
0N/A * operation</a>. It returns only after at least one channel is selected,
0N/A * this selector's {@link #wakeup wakeup} method is invoked, the current
0N/A * thread is interrupted, or the given timeout period expires, whichever
0N/A * comes first.
0N/A *
0N/A * <p> This method does not offer real-time guarantees: It schedules the
0N/A * timeout as if by invoking the {@link Object#wait(long)} method. </p>
0N/A *
0N/A * @param timeout If positive, block for up to <tt>timeout</tt>
0N/A * milliseconds, more or less, while waiting for a
0N/A * channel to become ready; if zero, block indefinitely;
0N/A * must not be negative
0N/A *
0N/A * @return The number of keys, possibly zero,
0N/A * whose ready-operation sets were updated
0N/A *
0N/A * @throws IOException
0N/A * If an I/O error occurs
0N/A *
0N/A * @throws ClosedSelectorException
0N/A * If this selector is closed
0N/A *
0N/A * @throws IllegalArgumentException
0N/A * If the value of the timeout argument is negative
0N/A */
0N/A public abstract int select(long timeout)
0N/A throws IOException;
0N/A
0N/A /**
0N/A * Selects a set of keys whose corresponding channels are ready for I/O
0N/A * operations.
0N/A *
0N/A * <p> This method performs a blocking <a href="#selop">selection
0N/A * operation</a>. It returns only after at least one channel is selected,
0N/A * this selector's {@link #wakeup wakeup} method is invoked, or the current
0N/A * thread is interrupted, whichever comes first. </p>
0N/A *
0N/A * @return The number of keys, possibly zero,
0N/A * whose ready-operation sets were updated
0N/A *
0N/A * @throws IOException
0N/A * If an I/O error occurs
0N/A *
0N/A * @throws ClosedSelectorException
0N/A * If this selector is closed
0N/A */
0N/A public abstract int select() throws IOException;
0N/A
0N/A /**
0N/A * Causes the first selection operation that has not yet returned to return
0N/A * immediately.
0N/A *
0N/A * <p> If another thread is currently blocked in an invocation of the
0N/A * {@link #select()} or {@link #select(long)} methods then that invocation
0N/A * will return immediately. If no selection operation is currently in
0N/A * progress then the next invocation of one of these methods will return
0N/A * immediately unless the {@link #selectNow()} method is invoked in the
0N/A * meantime. In any case the value returned by that invocation may be
0N/A * non-zero. Subsequent invocations of the {@link #select()} or {@link
0N/A * #select(long)} methods will block as usual unless this method is invoked
0N/A * again in the meantime.
0N/A *
0N/A * <p> Invoking this method more than once between two successive selection
0N/A * operations has the same effect as invoking it just once. </p>
0N/A *
0N/A * @return This selector
0N/A */
0N/A public abstract Selector wakeup();
0N/A
0N/A /**
0N/A * Closes this selector.
0N/A *
0N/A * <p> If a thread is currently blocked in one of this selector's selection
0N/A * methods then it is interrupted as if by invoking the selector's {@link
0N/A * #wakeup wakeup} method.
0N/A *
0N/A * <p> Any uncancelled keys still associated with this selector are
0N/A * invalidated, their channels are deregistered, and any other resources
0N/A * associated with this selector are released.
0N/A *
0N/A * <p> If this selector is already closed then invoking this method has no
0N/A * effect.
0N/A *
0N/A * <p> After a selector is closed, any further attempt to use it, except by
0N/A * invoking this method or the {@link #wakeup wakeup} method, will cause a
0N/A * {@link ClosedSelectorException} to be thrown. </p>
0N/A *
0N/A * @throws IOException
0N/A * If an I/O error occurs
0N/A */
0N/A public abstract void close() throws IOException;
0N/A
0N/A}