src/java.base/share/classes/java/nio/channels/Selector.java
Print this page
*** 27,37 ****
import java.io.Closeable;
import java.io.IOException;
import java.nio.channels.spi.SelectorProvider;
import java.util.Set;
!
/**
* A multiplexor of {@link SelectableChannel} objects.
*
* <p> A selector may be created by invoking the {@link #open open} method of
--- 27,37 ----
import java.io.Closeable;
import java.io.IOException;
import java.nio.channels.spi.SelectorProvider;
import java.util.Set;
! import java.util.function.Consumer;
/**
* A multiplexor of {@link SelectableChannel} objects.
*
* <p> A selector may be created by invoking the {@link #open open} method of
*** 295,304 ****
--- 295,338 ----
* If this selector is closed
*/
public abstract int selectNow() throws IOException;
/**
+ * Selects keys whose corresponding channels are ready for I/O
+ * operations and passes them to the provided consumer.
+ *
+ * <p> This method performs a non-blocking <a href="#selop">selection
+ * operation</a>. If no channels have become selectable since the previous
+ * selection operation then this method immediately returns zero.
+ *
+ * <p>The difference between this method and {@link #selectNow()} is that
+ * this method immediately passes the {@link java.nio.channels.SelectionKey} objects
+ * to its handler, rather than updating the key set.</p>
+ *
+ * <p> Invoking this method clears the effect of any previous invocations
+ * of the {@link #wakeup wakeup} method. </p>
+ *
+ * @return The number of keys, possibly zero, whose ready-operation sets
+ * were updated by the selection operation
+ *
+ * @param handler A handler function which is invoked with new SelectionKey
+ * objects When an selection operation occurs. It is called
+ * the same number of times as the return value.
+ *
+ * @throws java.lang.NullPointerException
+ * if the handler is null.
+ *
+ * @throws IOException
+ * If an I/O error occurs
+ *
+ * @throws ClosedSelectorException
+ * If this selector is closed
+ */
+ public abstract int selectNow(Consumer<SelectionKey> handler)
+ throws IOException;
+
+ /**
* Selects a set of keys whose corresponding channels are ready for I/O
* operations.
*
* <p> This method performs a blocking <a href="#selop">selection
* operation</a>. It returns only after at least one channel is selected,
*** 328,337 ****
--- 362,412 ----
*/
public abstract int select(long timeout)
throws IOException;
/**
+ * Selects keys whose corresponding channels are ready for I/O
+ * operations and passes them to the provided consumer.
+ *
+ * <p> This method performs a blocking <a href="#selop">selection
+ * operation</a>. It returns only after at least one channel is selected,
+ * this selector's {@link #wakeup wakeup} method is invoked, the current
+ * thread is interrupted, or the given timeout period expires, whichever
+ * comes first.
+ *
+ * <p>The difference between this method and {@link #select(long)} is that
+ * this method immediately passes the {@link java.nio.channels.SelectionKey} objects
+ * to its handler, rather than updating the key set.</p>
+ *
+ * <p> This method does not offer real-time guarantees: It schedules the
+ * timeout as if by invoking the {@link Object#wait(long)} method. </p>
+ *
+ * @param handler A handler function which is invoked with new SelectionKey
+ * objects When an selection operation occurs. It is called
+ * the same number of times as the return value.
+ *
+ * @param timeout If positive, block for up to <tt>timeout</tt>
+ * milliseconds, more or less, while waiting for a
+ * channel to become ready; if zero, block indefinitely;
+ * must not be negative
+ *
+ * @return The number of keys, possibly zero,
+ * whose ready-operation sets were updated
+ *
+ * @throws IOException
+ * If an I/O error occurs
+ *
+ * @throws ClosedSelectorException
+ * If this selector is closed
+ *
+ * @throws IllegalArgumentException
+ * If the value of the timeout argument is negative
+ */
+ public abstract int select(Consumer<SelectionKey> handler, long timeout)
+ throws IOException;
+
+ /**
* Selects a set of keys whose corresponding channels are ready for I/O
* operations.
*
* <p> This method performs a blocking <a href="#selop">selection
* operation</a>. It returns only after at least one channel is selected,