src/java.base/share/classes/java/nio/channels/Selector.java
author alanb
Wed, 18 Apr 2018 10:20:09 +0100
changeset 49802 8ac08fa69f00
parent 47216 71c04702a3d5
child 50602 ed8de3d0cd28
permissions -rw-r--r--
8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress Reviewed-by: bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4351
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4351
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4351
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4351
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4351
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
4351
c6b75a422eb8 6903753: (se) Selector should implement Closeable
alanb
parents: 2
diff changeset
    28
import java.io.Closeable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.spi.SelectorProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A multiplexor of {@link SelectableChannel} objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p> A selector may be created by invoking the {@link #open open} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * this class, which will use the system's default {@link
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    39
 * java.nio.channels.spi.SelectorProvider selector provider} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * create a new selector.  A selector may also be created by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * {@link java.nio.channels.spi.SelectorProvider#openSelector openSelector}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * method of a custom selector provider.  A selector remains open until it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * closed via its {@link #close close} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 32143
diff changeset
    45
 * <a id="ks"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> A selectable channel's registration with a selector is represented by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * {@link SelectionKey} object.  A selector maintains three sets of selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   <li><p> The <i>key set</i> contains the keys representing the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   channel registrations of this selector.  This set is returned by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   {@link #keys() keys} method. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   <li><p> The <i>selected-key set</i> is the set of keys such that each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   key's channel was detected to be ready for at least one of the operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   identified in the key's interest set during a prior selection operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   This set is returned by the {@link #selectedKeys() selectedKeys} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   The selected-key set is always a subset of the key set. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   <li><p> The <i>cancelled-key</i> set is the set of keys that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *   cancelled but whose channels have not yet been deregistered.  This set is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *   not directly accessible.  The cancelled-key set is always a subset of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *   key set. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p> All three sets are empty in a newly-created selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p> A key is added to a selector's key set as a side effect of registering a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * channel via the channel's {@link SelectableChannel#register(Selector,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * register} method.  Cancelled keys are removed from the key set during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * selection operations.  The key set itself is not directly modifiable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p> A key is added to its selector's cancelled-key set when it is cancelled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * whether by closing its channel or by invoking its {@link SelectionKey#cancel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * cancel} method.  Cancelling a key will cause its channel to be deregistered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * during the next selection operation, at which time the key will removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * all of the selector's key sets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 32143
diff changeset
    83
 * <a id="sks"></a><p> Keys are added to the selected-key set by selection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * operations.  A key may be removed directly from the selected-key set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * invoking the set's {@link java.util.Set#remove(java.lang.Object) remove}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * method or by invoking the {@link java.util.Iterator#remove() remove} method
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
    87
 * of an {@link java.util.Iterator iterator} obtained from the set.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
    88
 * All keys may be removed from the selected-key set by invoking the set's
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
    89
 * {@link java.util.Set#clear() clear} method.  Keys may not be added directly
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
    90
 * to the selected-key set. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 32143
diff changeset
    92
 * <a id="selop"></a>
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    93
 * <h2>Selection</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p> During each selection operation, keys may be added to and removed from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * selector's selected-key set and may be removed from its key and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * cancelled-key sets.  Selection is performed by the {@link #select()}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * #select(long)}, and {@link #selectNow()} methods, and involves three steps:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   <li><p> Each key in the cancelled-key set is removed from each key set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *   which it is a member, and its channel is deregistered.  This step leaves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *   the cancelled-key set empty. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *   <li><p> The underlying operating system is queried for an update as to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *   readiness of each remaining channel to perform any of the operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *   identified by its key's interest set as of the moment that the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *   operation began.  For a channel that is ready for at least one such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *   operation, one of the following two actions is performed: </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   113
 *   <ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *     <li><p> If the channel's key is not already in the selected-key set then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *     it is added to that set and its ready-operation set is modified to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *     identify exactly those operations for which the channel is now reported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *     to be ready.  Any readiness information previously recorded in the ready
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *     set is discarded.  </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *     <li><p> Otherwise the channel's key is already in the selected-key set,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *     so its ready-operation set is modified to identify any new operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *     for which the channel is reported to be ready.  Any readiness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *     information previously recorded in the ready set is preserved; in other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *     words, the ready set returned by the underlying system is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *     bitwise-disjoined into the key's current ready set. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   128
 *   </ol>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *   If all of the keys in the key set at the start of this step have empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *   interest sets then neither the selected-key set nor any of the keys'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *   ready-operation sets will be updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *   <li><p> If any keys were added to the cancelled-key set while step (2) was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *   in progress then they are processed as in step (1). </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <p> Whether or not a selection operation blocks to wait for one or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * channels to become ready, and if so for how long, is the only essential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * difference between the three selection methods. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   144
 * <h2>Concurrency</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   146
 * <p> A Selector and its key set are safe for use by multiple concurrent
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   147
 * threads.  Its selected-key set and cancelled-key set, however, are not.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   149
 * <p> The selection operations synchronize on the selector itself, on the
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   150
 * selected-key set, in that order.  They also synchronize on the cancelled-key
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   151
 * set during steps (1) and (3) above.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <p> Changes made to the interest sets of a selector's keys while a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * selection operation is in progress have no effect upon that operation; they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * will be seen by the next selection operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <p> Keys may be cancelled and channels may be closed at any time.  Hence the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * presence of a key in one or more of a selector's key sets does not imply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * that the key is valid or that its channel is open.  Application code should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * be careful to synchronize and check these conditions as necessary if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * is any possibility that another thread will cancel a key or close a channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <p> A thread blocked in one of the {@link #select()} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * #select(long)} methods may be interrupted by some other thread in one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * three ways:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *   <li><p> By invoking the selector's {@link #wakeup wakeup} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *   <li><p> By invoking the selector's {@link #close close} method, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *   </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *   <li><p> By invoking the blocked thread's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *   java.lang.Thread#interrupt() interrupt} method, in which case its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *   interrupt status will be set and the selector's {@link #wakeup wakeup}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *   method will be invoked. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   182
 * <p> The {@link #close close} method synchronizes on the selector and its
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   183
 * selected-key set in the same order as in a selection operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 32143
diff changeset
   185
 * <a id="ksc"></a>
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   186
 * <p> A Selector's key set is safe for use by multiple concurrent threads.
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   187
 * Retrieval operations from the key set do not generally block and so may
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   188
 * overlap with new registrations that add to the set, or with the cancellation
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   189
 * steps of selection operations that remove keys from the set.  Iterators and
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   190
 * spliterators return elements reflecting the state of the set at some point at
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   191
 * or since the creation of the iterator/spliterator.  They do not throw
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   192
 * {@link java.util.ConcurrentModificationException ConcurrentModificationException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   194
 * <a id="sksc"></a>
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   195
 * <p> A selector's selected-key set is not, in general, safe for use by
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   196
 * multiple concurrent threads.  If such a thread might modify the set directly
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   197
 * then access should be controlled by synchronizing on the set itself.  The
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   198
 * iterators returned by the set's {@link java.util.Set#iterator() iterator}
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   199
 * methods are <i>fail-fast:</i> If the set is modified after the iterator is
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   200
 * created, in any way except by invoking the iterator's own {@link
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   201
 * java.util.Iterator#remove() remove} method, then a {@link
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   202
 * java.util.ConcurrentModificationException} will be thrown. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * @see SelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * @see SelectionKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
4351
c6b75a422eb8 6903753: (se) Selector should implement Closeable
alanb
parents: 2
diff changeset
   212
public abstract class Selector implements Closeable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Initializes a new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    protected Selector() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Opens a selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <p> The new selector is created by invoking the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * java.nio.channels.spi.SelectorProvider#openSelector openSelector} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * of the system-wide default {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * java.nio.channels.spi.SelectorProvider} object.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @return  A new selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    public static Selector open() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        return SelectorProvider.provider().openSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   237
     * Tells whether or not this selector is open.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   239
     * @return {@code true} if, and only if, this selector is open
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public abstract boolean isOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 5506
diff changeset
   244
     * Returns the provider that created this channel.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return  The provider that created this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    public abstract SelectorProvider provider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * Returns this selector's key set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p> The key set is not directly modifiable.  A key is removed only after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * it has been cancelled and its channel has been deregistered.  Any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * attempt to modify the key set will cause an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * UnsupportedOperationException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   258
     * <p> The set is <a href="#ksc">safe</a> for use by multiple concurrent
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   259
     * threads.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return  This selector's key set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws  ClosedSelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *          If this selector is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public abstract Set<SelectionKey> keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * Returns this selector's selected-key set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * <p> Keys may be removed from, but not directly added to, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * selected-key set.  Any attempt to add an object to the key set will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * cause an {@link UnsupportedOperationException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   275
     * <p> The selected-key set is <a href="#sksc">not thread-safe</a>.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return  This selector's selected-key set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @throws  ClosedSelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *          If this selector is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public abstract Set<SelectionKey> selectedKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Selects a set of keys whose corresponding channels are ready for I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <p> This method performs a non-blocking <a href="#selop">selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * operation</a>.  If no channels have become selectable since the previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * selection operation then this method immediately returns zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p> Invoking this method clears the effect of any previous invocations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * of the {@link #wakeup wakeup} method.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return  The number of keys, possibly zero, whose ready-operation sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *          were updated by the selection operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @throws  ClosedSelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *          If this selector is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public abstract int selectNow() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Selects a set of keys whose corresponding channels are ready for I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <p> This method performs a blocking <a href="#selop">selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * operation</a>.  It returns only after at least one channel is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * this selector's {@link #wakeup wakeup} method is invoked, the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * thread is interrupted, or the given timeout period expires, whichever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * comes first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * <p> This method does not offer real-time guarantees: It schedules the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * timeout as if by invoking the {@link Object#wait(long)} method. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   319
     * @param  timeout  If positive, block for up to {@code timeout}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *                  milliseconds, more or less, while waiting for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *                  channel to become ready; if zero, block indefinitely;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *                  must not be negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @return  The number of keys, possibly zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *          whose ready-operation sets were updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @throws  ClosedSelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *          If this selector is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *          If the value of the timeout argument is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   336
    public abstract int select(long timeout) throws IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Selects a set of keys whose corresponding channels are ready for I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p> This method performs a blocking <a href="#selop">selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * operation</a>.  It returns only after at least one channel is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * this selector's {@link #wakeup wakeup} method is invoked, or the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * thread is interrupted, whichever comes first.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @return  The number of keys, possibly zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *          whose ready-operation sets were updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @throws  ClosedSelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *          If this selector is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    public abstract int select() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Causes the first selection operation that has not yet returned to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * immediately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * <p> If another thread is currently blocked in an invocation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * {@link #select()} or {@link #select(long)} methods then that invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * will return immediately.  If no selection operation is currently in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * progress then the next invocation of one of these methods will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * immediately unless the {@link #selectNow()} method is invoked in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * meantime.  In any case the value returned by that invocation may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * non-zero.  Subsequent invocations of the {@link #select()} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * #select(long)} methods will block as usual unless this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * again in the meantime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * <p> Invoking this method more than once between two successive selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * operations has the same effect as invoking it just once.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @return  This selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public abstract Selector wakeup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * Closes this selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <p> If a thread is currently blocked in one of this selector's selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * methods then it is interrupted as if by invoking the selector's {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * #wakeup wakeup} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p> Any uncancelled keys still associated with this selector are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * invalidated, their channels are deregistered, and any other resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * associated with this selector are released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * <p> If this selector is already closed then invoking this method has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * <p> After a selector is closed, any further attempt to use it, except by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * invoking this method or the {@link #wakeup wakeup} method, will cause a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * {@link ClosedSelectorException} to be thrown. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public abstract void close() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
}