KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > nio > channels > Selector


1 /*
2  * @(#)Selector.java 1.37 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.nio.channels;
9
10 import java.io.IOException JavaDoc;
11 import java.nio.channels.spi.SelectorProvider JavaDoc;
12 import java.util.Set JavaDoc;
13
14
15 /**
16  * A multiplexor of {@link SelectableChannel} objects.
17  *
18  * <p> A selector may be created by invoking the {@link #open open} method of
19  * this class, which will use the system's default {@link
20  * java.nio.channels.spi.SelectorProvider </code>selector provider<code>} to
21  * create a new selector. A selector may also be created by invoking the
22  * {@link java.nio.channels.spi.SelectorProvider#openSelector openSelector}
23  * method of a custom selector provider. A selector remains open until it is
24  * closed via its {@link #close close} method.
25  *
26  * <a name="ks">
27  *
28  * <p> A selectable channel's registration with a selector is represented by a
29  * {@link SelectionKey} object. A selector maintains three sets of selection
30  * keys:
31  *
32  * <ul>
33  *
34  * <li><p> The <i>key set</i> contains the keys representing the current
35  * channel registrations of this selector. This set is returned by the
36  * {@link #keys() keys} method. </p></li>
37  *
38  * <li><p> The <i>selected-key set</i> is the set of keys such that each
39  * key's channel was detected to be ready for at least one of the operations
40  * identified in the key's interest set during a prior selection operation.
41  * This set is returned by the {@link #selectedKeys() selectedKeys} method.
42  * The selected-key set is always a subset of the key set. </p></li>
43  *
44  * <li><p> The <i>cancelled-key</i> set is the set of keys that have been
45  * cancelled but whose channels have not yet been deregistered. This set is
46  * not directly accessible. The cancelled-key set is always a subset of the
47  * key set. </p></li>
48  *
49  * </ul>
50  *
51  * <p> All three sets are empty in a newly-created selector.
52  *
53  * <p> A key is added to a selector's key set as a side effect of registering a
54  * channel via the channel's {@link SelectableChannel#register(Selector,int)
55  * register} method. Cancelled keys are removed from the key set during
56  * selection operations. The key set itself is not directly modifiable.
57  *
58  * <p> A key is added to its selector's cancelled-key set when it is cancelled,
59  * whether by closing its channel or by invoking its {@link SelectionKey#cancel
60  * cancel} method. Cancelling a key will cause its channel to be deregistered
61  * during the next selection operation, at which time the key will removed from
62  * all of the selector's key sets.
63  *
64  * <a name="sks"><p> Keys are added to the selected-key set by selection
65  * operations. A key may be removed directly from the selected-key set by
66  * invoking the set's {@link java.util.Set#remove(java.lang.Object) remove}
67  * method or by invoking the {@link java.util.Iterator#remove() remove} method
68  * of an {@link java.util.Iterator </code>iterator<code>} obtained from the
69  * set. Keys are never removed from the selected-key set in any other way;
70  * they are not, in particular, removed as a side effect of selection
71  * operations. Keys may not be added directly to the selected-key set. </p>
72  *
73  *
74  * <a name="selop">
75  * <h4>Selection</h4>
76  *
77  * <p> During each selection operation, keys may be added to and removed from a
78  * selector's selected-key set and may be removed from its key and
79  * cancelled-key sets. Selection is performed by the {@link #select()}, {@link
80  * #select(long)}, and {@link #selectNow()} methods, and involves three steps:
81  * </p>
82  *
83  * <ol>
84  *
85  * <li><p> Each key in the cancelled-key set is removed from each key set of
86  * which it is a member, and its channel is deregistered. This step leaves
87  * the cancelled-key set empty. </p></li>
88  *
89  * <li><p> The underlying operating system is queried for an update as to the
90  * readiness of each remaining channel to perform any of the operations
91  * identified by its key's interest set as of the moment that the selection
92  * operation began. For a channel that is ready for at least one such
93  * operation, one of the following two actions is performed: </p>
94  *
95  * <ol type=a>
96  *
97  * <li><p> If the channel's key is not already in the selected-key set then
98  * it is added to that set and its ready-operation set is modified to
99  * identify exactly those operations for which the channel is now reported
100  * to be ready. Any readiness information previously recorded in the ready
101  * set is discarded. </p></li>
102  *
103  * <li><p> Otherwise the channel's key is already in the selected-key set,
104  * so its ready-operation set is modified to identify any new operations
105  * for which the channel is reported to be ready. Any readiness
106  * information previously recorded in the ready set is preserved; in other
107  * words, the ready set returned by the underlying system is
108  * bitwise-disjoined into the key's current ready set. </p></li>
109  *
110  * </ol></li>
111  *
112  * If all of the keys in the key set at the start of this step have empty
113  * interest sets then neither the selected-key set nor any of the keys'
114  * ready-operation sets will be updated.
115  *
116  * <li><p> If any keys were added to the cancelled-key set while step (2) was
117  * in progress then they are processed as in step (1). </p></li>
118  *
119  * </ol>
120  *
121  * <p> Whether or not a selection operation blocks to wait for one or more
122  * channels to become ready, and if so for how long, is the only essential
123  * difference between the three selection methods. </p>
124  *
125  *
126  * <h4>Concurrency</h4>
127  *
128  * <p> Selectors are themselves safe for use by multiple concurrent threads;
129  * their key sets, however, are not.
130  *
131  * <p> The selection operations synchronize on the selector itself, on the key
132  * set, and on the selected-key set, in that order. They also synchronize on
133  * the cancelled-key set during steps (1) and (3) above.
134  *
135  * <p> Changes made to the interest sets of a selector's keys while a
136  * selection operation is in progress have no effect upon that operation; they
137  * will be seen by the next selection operation.
138  *
139  * <p> Keys may be cancelled and channels may be closed at any time. Hence the
140  * presence of a key in one or more of a selector's key sets does not imply
141  * that the key is valid or that its channel is open. Application code should
142  * be careful to synchronize and check these conditions as necessary if there
143  * is any possibility that another thread will cancel a key or close a channel.
144  *
145  * <p> A thread blocked in one of the {@link #select()} or {@link
146  * #select(long)} methods may be interrupted by some other thread in one of
147  * three ways:
148  *
149  * <ul>
150  *
151  * <li><p> By invoking the selector's {@link #wakeup wakeup} method,
152  * </p></li>
153  *
154  * <li><p> By invoking the selector's {@link #close close} method, or
155  * </p></li>
156  *
157  * <li><p> By invoking the blocked thread's {@link
158  * java.lang.Thread#interrupt() interrupt} method, in which case its
159  * interrupt status will be set and the selector's {@link #wakeup wakeup}
160  * method will be invoked. </p></li>
161  *
162  * </ul>
163  *
164  * <p> The {@link #close close} method synchronizes on the selector and all
165  * three key sets in the same order as in a selection operation.
166  *
167  * <a name="ksc">
168  *
169  * <p> A selector's key and selected-key sets are not, in general, safe for use
170  * by multiple concurrent threads. If such a thread might modify one of these
171  * sets directly then access should be controlled by synchronizing on the set
172  * itself. The iterators returned by these sets' {@link
173  * java.util.Set#iterator() iterator} methods are <i>fail-fast:</i> If the set
174  * is modified after the iterator is created, in any way except by invoking the
175  * iterator's own {@link java.util.Iterator#remove() remove} method, then a
176  * {@link java.util.ConcurrentModificationException} will be thrown. </p>
177  *
178  *
179  * @author Mark Reinhold
180  * @author JSR-51 Expert Group
181  * @version 1.37, 04/05/05
182  * @since 1.4
183  *
184  * @see SelectableChannel
185  * @see SelectionKey
186  */

187
188 public abstract class Selector {
189
190     /**
191      * Initializes a new instance of this class.
192      */

193     protected Selector() { }
194
195     /**
196      * Opens a selector.
197      *
198      * <p> The new selector is created by invoking the {@link
199      * java.nio.channels.spi.SelectorProvider#openSelector openSelector} method
200      * of the system-wide default {@link
201      * java.nio.channels.spi.SelectorProvider} object. </p>
202      *
203      * @return A new selector
204      *
205      * @throws IOException
206      * If an I/O error occurs
207      */

208     public static Selector JavaDoc open() throws IOException JavaDoc {
209     return SelectorProvider.provider().openSelector();
210     }
211
212     /**
213      * Tells whether or not this selector is open. </p>
214      *
215      * @return <tt>true</tt> if, and only if, this selector is open
216      */

217     public abstract boolean isOpen();
218
219     /**
220      * Returns the provider that created this channel. </p>
221      *
222      * @return The provider that created this channel
223      */

224     public abstract SelectorProvider JavaDoc provider();
225
226     /**
227      * Returns this selector's key set.
228      *
229      * <p> The key set is not directly modifiable. A key is removed only after
230      * it has been cancelled and its channel has been deregistered. Any
231      * attempt to modify the key set will cause an {@link
232      * UnsupportedOperationException} to be thrown.
233      *
234      * <p> The key set is <a HREF="#ksc">not thread-safe</a>. </p>
235      *
236      * @return This selector's key set
237      *
238      * @throws ClosedSelectorException
239      * If this selector is closed
240      */

241     public abstract Set JavaDoc<SelectionKey JavaDoc> keys();
242
243     /**
244      * Returns this selector's selected-key set.
245      *
246      * <p> Keys may be removed from, but not directly added to, the
247      * selected-key set. Any attempt to add an object to the key set will
248      * cause an {@link UnsupportedOperationException} to be thrown.
249      *
250      * <p> The selected-key set is <a HREF="#ksc">not thread-safe</a>. </p>
251      *
252      * @return This selector's selected-key set
253      *
254      * @throws ClosedSelectorException
255      * If this selector is closed
256      */

257     public abstract Set JavaDoc<SelectionKey JavaDoc> selectedKeys();
258
259     /**
260      * Selects a set of keys whose corresponding channels are ready for I/O
261      * operations.
262      *
263      * <p> This method performs a non-blocking <a HREF="#selop">selection
264      * operation</a>. If no channels have become selectable since the previous
265      * selection operation then this method immediately returns zero.
266      *
267      * <p> Invoking this method clears the effect of any previous invocations
268      * of the {@link #wakeup wakeup} method. </p>
269      *
270      * @return The number of keys, possibly zero, whose ready-operation sets
271      * were updated by the selection operation
272      *
273      * @throws IOException
274      * If an I/O error occurs
275      *
276      * @throws ClosedSelectorException
277      * If this selector is closed
278      */

279     public abstract int selectNow() throws IOException JavaDoc;
280
281     /**
282      * Selects a set of keys whose corresponding channels are ready for I/O
283      * operations.
284      *
285      * <p> This method performs a blocking <a HREF="#selop">selection
286      * operation</a>. It returns only after at least one channel is selected,
287      * this selector's {@link #wakeup wakeup} method is invoked, the current
288      * thread is interrupted, or the given timeout period expires, whichever
289      * comes first.
290      *
291      * <p> This method does not offer real-time guarantees: It schedules the
292      * timeout as if by invoking the {@link Object#wait(long)} method. </p>
293      *
294      * @param timeout If positive, block for up to <tt>timeout</tt>
295      * milliseconds, more or less, while waiting for a
296      * channel to become ready; if zero, block indefinitely;
297      * must not be negative
298      *
299      * @return The number of keys, possibly zero,
300      * whose ready-operation sets were updated
301      *
302      * @throws IOException
303      * If an I/O error occurs
304      *
305      * @throws ClosedSelectorException
306      * If this selector is closed
307      *
308      * @throws IllegalArgumentException
309      * If the value of the timeout argument is negative
310      */

311     public abstract int select(long timeout)
312     throws IOException JavaDoc;
313
314     /**
315      * Selects a set of keys whose corresponding channels are ready for I/O
316      * operations.
317      *
318      * <p> This method performs a blocking <a HREF="#selop">selection
319      * operation</a>. It returns only after at least one channel is selected,
320      * this selector's {@link #wakeup wakeup} method is invoked, or the current
321      * thread is interrupted, whichever comes first. </p>
322      *
323      * @return The number of keys, possibly zero,
324      * whose ready-operation sets were updated
325      *
326      * @throws IOException
327      * If an I/O error occurs
328      *
329      * @throws ClosedSelectorException
330      * If this selector is closed
331      */

332     public abstract int select() throws IOException JavaDoc;
333
334     /**
335      * Causes the first selection operation that has not yet returned to return
336      * immediately.
337      *
338      * <p> If another thread is currently blocked in an invocation of the
339      * {@link #select()} or {@link #select(long)} methods then that invocation
340      * will return immediately. If no selection operation is currently in
341      * progress then the next invocation of one of these methods will return
342      * immediately unless the {@link #selectNow()} method is invoked in the
343      * meantime. In any case the value returned by that invocation may be
344      * non-zero. Subsequent invocations of the {@link #select()} or {@link
345      * #select(long)} methods will block as usual unless this method is invoked
346      * again in the meantime.
347      *
348      * <p> Invoking this method more than once between two successive selection
349      * operations has the same effect as invoking it just once. </p>
350      *
351      * @return This selector
352      */

353     public abstract Selector JavaDoc wakeup();
354
355     /**
356      * Closes this selector.
357      *
358      * <p> If a thread is currently blocked in one of this selector's selection
359      * methods then it is interrupted as if by invoking the selector's {@link
360      * #wakeup wakeup} method.
361      *
362      * <p> Any uncancelled keys still associated with this selector are
363      * invalidated, their channels are deregistered, and any other resources
364      * associated with this selector are released.
365      *
366      * <p> If this selector is already closed then invoking this method has no
367      * effect.
368      *
369      * <p> After a selector is closed, any further attempt to use it, except by
370      * invoking this method or the {@link #wakeup wakeup} method, will cause a
371      * {@link ClosedSelectorException} to be thrown. </p>
372      *
373      * @throws IOException
374      * If an I/O error occurs
375      */

376     public abstract void close() throws IOException JavaDoc;
377
378 }
379
Popular Tags