001 /*
002 * Copyright 2000-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.nio.channels;
027
028 import java.io.IOException;
029 import java.nio.channels.spi.SelectorProvider;
030 import java.util.Set;
031
032 /**
033 * A multiplexor of {@link SelectableChannel} objects.
034 *
035 * <p> A selector may be created by invoking the {@link #open open} method of
036 * this class, which will use the system's default {@link
037 * java.nio.channels.spi.SelectorProvider </code>selector provider<code>} to
038 * create a new selector. A selector may also be created by invoking the
039 * {@link java.nio.channels.spi.SelectorProvider#openSelector openSelector}
040 * method of a custom selector provider. A selector remains open until it is
041 * closed via its {@link #close close} method.
042 *
043 * <a name="ks">
044 *
045 * <p> A selectable channel's registration with a selector is represented by a
046 * {@link SelectionKey} object. A selector maintains three sets of selection
047 * keys:
048 *
049 * <ul>
050 *
051 * <li><p> The <i>key set</i> contains the keys representing the current
052 * channel registrations of this selector. This set is returned by the
053 * {@link #keys() keys} method. </p></li>
054 *
055 * <li><p> The <i>selected-key set</i> is the set of keys such that each
056 * key's channel was detected to be ready for at least one of the operations
057 * identified in the key's interest set during a prior selection operation.
058 * This set is returned by the {@link #selectedKeys() selectedKeys} method.
059 * The selected-key set is always a subset of the key set. </p></li>
060 *
061 * <li><p> The <i>cancelled-key</i> set is the set of keys that have been
062 * cancelled but whose channels have not yet been deregistered. This set is
063 * not directly accessible. The cancelled-key set is always a subset of the
064 * key set. </p></li>
065 *
066 * </ul>
067 *
068 * <p> All three sets are empty in a newly-created selector.
069 *
070 * <p> A key is added to a selector's key set as a side effect of registering a
071 * channel via the channel's {@link SelectableChannel#register(Selector,int)
072 * register} method. Cancelled keys are removed from the key set during
073 * selection operations. The key set itself is not directly modifiable.
074 *
075 * <p> A key is added to its selector's cancelled-key set when it is cancelled,
076 * whether by closing its channel or by invoking its {@link SelectionKey#cancel
077 * cancel} method. Cancelling a key will cause its channel to be deregistered
078 * during the next selection operation, at which time the key will removed from
079 * all of the selector's key sets.
080 *
081 * <a name="sks"><p> Keys are added to the selected-key set by selection
082 * operations. A key may be removed directly from the selected-key set by
083 * invoking the set's {@link java.util.Set#remove(java.lang.Object) remove}
084 * method or by invoking the {@link java.util.Iterator#remove() remove} method
085 * of an {@link java.util.Iterator </code>iterator<code>} obtained from the
086 * set. Keys are never removed from the selected-key set in any other way;
087 * they are not, in particular, removed as a side effect of selection
088 * operations. Keys may not be added directly to the selected-key set. </p>
089 *
090 *
091 * <a name="selop">
092 * <h4>Selection</h4>
093 *
094 * <p> During each selection operation, keys may be added to and removed from a
095 * selector's selected-key set and may be removed from its key and
096 * cancelled-key sets. Selection is performed by the {@link #select()}, {@link
097 * #select(long)}, and {@link #selectNow()} methods, and involves three steps:
098 * </p>
099 *
100 * <ol>
101 *
102 * <li><p> Each key in the cancelled-key set is removed from each key set of
103 * which it is a member, and its channel is deregistered. This step leaves
104 * the cancelled-key set empty. </p></li>
105 *
106 * <li><p> The underlying operating system is queried for an update as to the
107 * readiness of each remaining channel to perform any of the operations
108 * identified by its key's interest set as of the moment that the selection
109 * operation began. For a channel that is ready for at least one such
110 * operation, one of the following two actions is performed: </p>
111 *
112 * <ol type=a>
113 *
114 * <li><p> If the channel's key is not already in the selected-key set then
115 * it is added to that set and its ready-operation set is modified to
116 * identify exactly those operations for which the channel is now reported
117 * to be ready. Any readiness information previously recorded in the ready
118 * set is discarded. </p></li>
119 *
120 * <li><p> Otherwise the channel's key is already in the selected-key set,
121 * so its ready-operation set is modified to identify any new operations
122 * for which the channel is reported to be ready. Any readiness
123 * information previously recorded in the ready set is preserved; in other
124 * words, the ready set returned by the underlying system is
125 * bitwise-disjoined into the key's current ready set. </p></li>
126 *
127 * </ol></li>
128 *
129 * If all of the keys in the key set at the start of this step have empty
130 * interest sets then neither the selected-key set nor any of the keys'
131 * ready-operation sets will be updated.
132 *
133 * <li><p> If any keys were added to the cancelled-key set while step (2) was
134 * in progress then they are processed as in step (1). </p></li>
135 *
136 * </ol>
137 *
138 * <p> Whether or not a selection operation blocks to wait for one or more
139 * channels to become ready, and if so for how long, is the only essential
140 * difference between the three selection methods. </p>
141 *
142 *
143 * <h4>Concurrency</h4>
144 *
145 * <p> Selectors are themselves safe for use by multiple concurrent threads;
146 * their key sets, however, are not.
147 *
148 * <p> The selection operations synchronize on the selector itself, on the key
149 * set, and on the selected-key set, in that order. They also synchronize on
150 * the cancelled-key set during steps (1) and (3) above.
151 *
152 * <p> Changes made to the interest sets of a selector's keys while a
153 * selection operation is in progress have no effect upon that operation; they
154 * will be seen by the next selection operation.
155 *
156 * <p> Keys may be cancelled and channels may be closed at any time. Hence the
157 * presence of a key in one or more of a selector's key sets does not imply
158 * that the key is valid or that its channel is open. Application code should
159 * be careful to synchronize and check these conditions as necessary if there
160 * is any possibility that another thread will cancel a key or close a channel.
161 *
162 * <p> A thread blocked in one of the {@link #select()} or {@link
163 * #select(long)} methods may be interrupted by some other thread in one of
164 * three ways:
165 *
166 * <ul>
167 *
168 * <li><p> By invoking the selector's {@link #wakeup wakeup} method,
169 * </p></li>
170 *
171 * <li><p> By invoking the selector's {@link #close close} method, or
172 * </p></li>
173 *
174 * <li><p> By invoking the blocked thread's {@link
175 * java.lang.Thread#interrupt() interrupt} method, in which case its
176 * interrupt status will be set and the selector's {@link #wakeup wakeup}
177 * method will be invoked. </p></li>
178 *
179 * </ul>
180 *
181 * <p> The {@link #close close} method synchronizes on the selector and all
182 * three key sets in the same order as in a selection operation.
183 *
184 * <a name="ksc">
185 *
186 * <p> A selector's key and selected-key sets are not, in general, safe for use
187 * by multiple concurrent threads. If such a thread might modify one of these
188 * sets directly then access should be controlled by synchronizing on the set
189 * itself. The iterators returned by these sets' {@link
190 * java.util.Set#iterator() iterator} methods are <i>fail-fast:</i> If the set
191 * is modified after the iterator is created, in any way except by invoking the
192 * iterator's own {@link java.util.Iterator#remove() remove} method, then a
193 * {@link java.util.ConcurrentModificationException} will be thrown. </p>
194 *
195 *
196 * @author Mark Reinhold
197 * @author JSR-51 Expert Group
198 * @version 1.44, 07/05/05
199 * @since 1.4
200 *
201 * @see SelectableChannel
202 * @see SelectionKey
203 */
204
205 public abstract class Selector {
206
207 /**
208 * Initializes a new instance of this class.
209 */
210 protected Selector() {
211 }
212
213 /**
214 * Opens a selector.
215 *
216 * <p> The new selector is created by invoking the {@link
217 * java.nio.channels.spi.SelectorProvider#openSelector openSelector} method
218 * of the system-wide default {@link
219 * java.nio.channels.spi.SelectorProvider} object. </p>
220 *
221 * @return A new selector
222 *
223 * @throws IOException
224 * If an I/O error occurs
225 */
226 public static Selector open() throws IOException {
227 return SelectorProvider.provider().openSelector();
228 }
229
230 /**
231 * Tells whether or not this selector is open. </p>
232 *
233 * @return <tt>true</tt> if, and only if, this selector is open
234 */
235 public abstract boolean isOpen();
236
237 /**
238 * Returns the provider that created this channel. </p>
239 *
240 * @return The provider that created this channel
241 */
242 public abstract SelectorProvider provider();
243
244 /**
245 * Returns this selector's key set.
246 *
247 * <p> The key set is not directly modifiable. A key is removed only after
248 * it has been cancelled and its channel has been deregistered. Any
249 * attempt to modify the key set will cause an {@link
250 * UnsupportedOperationException} to be thrown.
251 *
252 * <p> The key set is <a href="#ksc">not thread-safe</a>. </p>
253 *
254 * @return This selector's key set
255 *
256 * @throws ClosedSelectorException
257 * If this selector is closed
258 */
259 public abstract Set<SelectionKey> keys();
260
261 /**
262 * Returns this selector's selected-key set.
263 *
264 * <p> Keys may be removed from, but not directly added to, the
265 * selected-key set. Any attempt to add an object to the key set will
266 * cause an {@link UnsupportedOperationException} to be thrown.
267 *
268 * <p> The selected-key set is <a href="#ksc">not thread-safe</a>. </p>
269 *
270 * @return This selector's selected-key set
271 *
272 * @throws ClosedSelectorException
273 * If this selector is closed
274 */
275 public abstract Set<SelectionKey> selectedKeys();
276
277 /**
278 * Selects a set of keys whose corresponding channels are ready for I/O
279 * operations.
280 *
281 * <p> This method performs a non-blocking <a href="#selop">selection
282 * operation</a>. If no channels have become selectable since the previous
283 * selection operation then this method immediately returns zero.
284 *
285 * <p> Invoking this method clears the effect of any previous invocations
286 * of the {@link #wakeup wakeup} method. </p>
287 *
288 * @return The number of keys, possibly zero, whose ready-operation sets
289 * were updated by the selection operation
290 *
291 * @throws IOException
292 * If an I/O error occurs
293 *
294 * @throws ClosedSelectorException
295 * If this selector is closed
296 */
297 public abstract int selectNow() throws IOException;
298
299 /**
300 * Selects a set of keys whose corresponding channels are ready for I/O
301 * operations.
302 *
303 * <p> This method performs a blocking <a href="#selop">selection
304 * operation</a>. It returns only after at least one channel is selected,
305 * this selector's {@link #wakeup wakeup} method is invoked, the current
306 * thread is interrupted, or the given timeout period expires, whichever
307 * comes first.
308 *
309 * <p> This method does not offer real-time guarantees: It schedules the
310 * timeout as if by invoking the {@link Object#wait(long)} method. </p>
311 *
312 * @param timeout If positive, block for up to <tt>timeout</tt>
313 * milliseconds, more or less, while waiting for a
314 * channel to become ready; if zero, block indefinitely;
315 * must not be negative
316 *
317 * @return The number of keys, possibly zero,
318 * whose ready-operation sets were updated
319 *
320 * @throws IOException
321 * If an I/O error occurs
322 *
323 * @throws ClosedSelectorException
324 * If this selector is closed
325 *
326 * @throws IllegalArgumentException
327 * If the value of the timeout argument is negative
328 */
329 public abstract int select(long timeout) throws IOException;
330
331 /**
332 * Selects a set of keys whose corresponding channels are ready for I/O
333 * operations.
334 *
335 * <p> This method performs a blocking <a href="#selop">selection
336 * operation</a>. It returns only after at least one channel is selected,
337 * this selector's {@link #wakeup wakeup} method is invoked, or the current
338 * thread is interrupted, whichever comes first. </p>
339 *
340 * @return The number of keys, possibly zero,
341 * whose ready-operation sets were updated
342 *
343 * @throws IOException
344 * If an I/O error occurs
345 *
346 * @throws ClosedSelectorException
347 * If this selector is closed
348 */
349 public abstract int select() throws IOException;
350
351 /**
352 * Causes the first selection operation that has not yet returned to return
353 * immediately.
354 *
355 * <p> If another thread is currently blocked in an invocation of the
356 * {@link #select()} or {@link #select(long)} methods then that invocation
357 * will return immediately. If no selection operation is currently in
358 * progress then the next invocation of one of these methods will return
359 * immediately unless the {@link #selectNow()} method is invoked in the
360 * meantime. In any case the value returned by that invocation may be
361 * non-zero. Subsequent invocations of the {@link #select()} or {@link
362 * #select(long)} methods will block as usual unless this method is invoked
363 * again in the meantime.
364 *
365 * <p> Invoking this method more than once between two successive selection
366 * operations has the same effect as invoking it just once. </p>
367 *
368 * @return This selector
369 */
370 public abstract Selector wakeup();
371
372 /**
373 * Closes this selector.
374 *
375 * <p> If a thread is currently blocked in one of this selector's selection
376 * methods then it is interrupted as if by invoking the selector's {@link
377 * #wakeup wakeup} method.
378 *
379 * <p> Any uncancelled keys still associated with this selector are
380 * invalidated, their channels are deregistered, and any other resources
381 * associated with this selector are released.
382 *
383 * <p> If this selector is already closed then invoking this method has no
384 * effect.
385 *
386 * <p> After a selector is closed, any further attempt to use it, except by
387 * invoking this method or the {@link #wakeup wakeup} method, will cause a
388 * {@link ClosedSelectorException} to be thrown. </p>
389 *
390 * @throws IOException
391 * If an I/O error occurs
392 */
393 public abstract void close() throws IOException;
394
395 }
|