Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/nio/channels/AsynchronousChannelGroup.java
41159 views
1
/*
2
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.nio.channels;
27
28
import java.nio.channels.spi.AsynchronousChannelProvider;
29
import java.io.IOException;
30
import java.util.concurrent.ExecutorService;
31
import java.util.concurrent.ThreadFactory;
32
import java.util.concurrent.TimeUnit;
33
34
/**
35
* A grouping of asynchronous channels for the purpose of resource sharing.
36
*
37
* <p> An asynchronous channel group encapsulates the mechanics required to
38
* handle the completion of I/O operations initiated by {@link AsynchronousChannel
39
* asynchronous channels} that are bound to the group. A group has an associated
40
* thread pool to which tasks are submitted to handle I/O events and dispatch to
41
* {@link CompletionHandler completion-handlers} that consume the result of
42
* asynchronous operations performed on channels in the group. In addition to
43
* handling I/O events, the pooled threads may also execute other tasks required
44
* to support the execution of asynchronous I/O operations.
45
*
46
* <p> An asynchronous channel group is created by invoking the {@link
47
* #withFixedThreadPool withFixedThreadPool} or {@link #withCachedThreadPool
48
* withCachedThreadPool} methods defined here. Channels are bound to a group by
49
* specifying the group when constructing the channel. The associated thread
50
* pool is <em>owned</em> by the group; termination of the group results in the
51
* shutdown of the associated thread pool.
52
*
53
* <p> In addition to groups created explicitly, the Java virtual machine
54
* maintains a system-wide <em>default group</em> that is constructed
55
* automatically. Asynchronous channels that do not specify a group at
56
* construction time are bound to the default group. The default group has an
57
* associated thread pool that creates new threads as needed. The default group
58
* may be configured by means of system properties defined in the table below.
59
* Where the {@link java.util.concurrent.ThreadFactory ThreadFactory} for the
60
* default group is not configured then the pooled threads of the default group
61
* are {@link Thread#isDaemon daemon} threads.
62
*
63
* <table class="striped">
64
* <caption style="display:none:">System properties</caption>
65
* <thead>
66
* <tr>
67
* <th scope="col">System property</th>
68
* <th scope="col">Description</th>
69
* </tr>
70
* </thead>
71
* <tbody>
72
* <tr>
73
* <th scope="row">
74
* {@systemProperty java.nio.channels.DefaultThreadPool.threadFactory}
75
* </th>
76
* <td> The value of this property is taken to be the fully-qualified name
77
* of a concrete {@link java.util.concurrent.ThreadFactory ThreadFactory}
78
* class. The class is loaded using the system class loader and instantiated.
79
* The factory's {@link java.util.concurrent.ThreadFactory#newThread
80
* newThread} method is invoked to create each thread for the default
81
* group's thread pool. If the process to load and instantiate the value
82
* of the property fails then an unspecified error is thrown during the
83
* construction of the default group. </td>
84
* </tr>
85
* <tr>
86
* <th scope="row">
87
* {@systemProperty java.nio.channels.DefaultThreadPool.initialSize}
88
* </th>
89
* <td> The value of the {@code initialSize} parameter for the default
90
* group (see {@link #withCachedThreadPool withCachedThreadPool}).
91
* The value of the property is taken to be the {@code String}
92
* representation of an {@code Integer} that is the initial size parameter.
93
* If the value cannot be parsed as an {@code Integer} it causes an
94
* unspecified error to be thrown during the construction of the default
95
* group. </td>
96
* </tr>
97
* </tbody>
98
* </table>
99
*
100
* <a id="threading"></a><h2>Threading</h2>
101
*
102
* <p> The completion handler for an I/O operation initiated on a channel bound
103
* to a group is guaranteed to be invoked by one of the pooled threads in the
104
* group. This ensures that the completion handler is run by a thread with the
105
* expected <em>identity</em>.
106
*
107
* <p> Where an I/O operation completes immediately, and the initiating thread
108
* is one of the pooled threads in the group then the completion handler may
109
* be invoked directly by the initiating thread. To avoid stack overflow, an
110
* implementation may impose a limit as to the number of activations on the
111
* thread stack. Some I/O operations may prohibit invoking the completion
112
* handler directly by the initiating thread (see {@link
113
* AsynchronousServerSocketChannel#accept(Object,CompletionHandler) accept}).
114
*
115
* <a id="shutdown"></a><h2>Shutdown and Termination</h2>
116
*
117
* <p> The {@link #shutdown() shutdown} method is used to initiate an <em>orderly
118
* shutdown</em> of a group. An orderly shutdown marks the group as shutdown;
119
* further attempts to construct a channel that binds to the group will throw
120
* {@link ShutdownChannelGroupException}. Whether or not a group is shutdown can
121
* be tested using the {@link #isShutdown() isShutdown} method. Once shutdown,
122
* the group <em>terminates</em> when all asynchronous channels that are bound to
123
* the group are closed, all actively executing completion handlers have run to
124
* completion, and resources used by the group are released. No attempt is made
125
* to stop or interrupt threads that are executing completion handlers. The
126
* {@link #isTerminated() isTerminated} method is used to test if the group has
127
* terminated, and the {@link #awaitTermination awaitTermination} method can be
128
* used to block until the group has terminated.
129
*
130
* <p> The {@link #shutdownNow() shutdownNow} method can be used to initiate a
131
* <em>forceful shutdown</em> of the group. In addition to the actions performed
132
* by an orderly shutdown, the {@code shutdownNow} method closes all open channels
133
* in the group as if by invoking the {@link AsynchronousChannel#close close}
134
* method.
135
*
136
* @since 1.7
137
*
138
* @see AsynchronousSocketChannel#open(AsynchronousChannelGroup)
139
* @see AsynchronousServerSocketChannel#open(AsynchronousChannelGroup)
140
*/
141
142
public abstract class AsynchronousChannelGroup {
143
private final AsynchronousChannelProvider provider;
144
145
/**
146
* Initialize a new instance of this class.
147
*
148
* @param provider
149
* The asynchronous channel provider for this group
150
*/
151
protected AsynchronousChannelGroup(AsynchronousChannelProvider provider) {
152
this.provider = provider;
153
}
154
155
/**
156
* Returns the provider that created this channel group.
157
*
158
* @return The provider that created this channel group
159
*/
160
public final AsynchronousChannelProvider provider() {
161
return provider;
162
}
163
164
/**
165
* Creates an asynchronous channel group with a fixed thread pool.
166
*
167
* <p> The resulting asynchronous channel group reuses a fixed number of
168
* threads. At any point, at most {@code nThreads} threads will be active
169
* processing tasks that are submitted to handle I/O events and dispatch
170
* completion results for operations initiated on asynchronous channels in
171
* the group.
172
*
173
* <p> The group is created by invoking the {@link
174
* AsynchronousChannelProvider#openAsynchronousChannelGroup(int,ThreadFactory)
175
* openAsynchronousChannelGroup(int,ThreadFactory)} method of the system-wide
176
* default {@link AsynchronousChannelProvider} object.
177
*
178
* @param nThreads
179
* The number of threads in the pool
180
* @param threadFactory
181
* The factory to use when creating new threads
182
*
183
* @return A new asynchronous channel group
184
*
185
* @throws IllegalArgumentException
186
* If {@code nThreads <= 0}
187
* @throws IOException
188
* If an I/O error occurs
189
*/
190
public static AsynchronousChannelGroup withFixedThreadPool(int nThreads,
191
ThreadFactory threadFactory)
192
throws IOException
193
{
194
return AsynchronousChannelProvider.provider()
195
.openAsynchronousChannelGroup(nThreads, threadFactory);
196
}
197
198
/**
199
* Creates an asynchronous channel group with a given thread pool that
200
* creates new threads as needed.
201
*
202
* <p> The {@code executor} parameter is an {@code ExecutorService} that
203
* creates new threads as needed to execute tasks that are submitted to
204
* handle I/O events and dispatch completion results for operations initiated
205
* on asynchronous channels in the group. It may reuse previously constructed
206
* threads when they are available.
207
*
208
* <p> The {@code initialSize} parameter may be used by the implementation
209
* as a <em>hint</em> as to the initial number of tasks it may submit. For
210
* example, it may be used to indicate the initial number of threads that
211
* wait on I/O events.
212
*
213
* <p> The executor is intended to be used exclusively by the resulting
214
* asynchronous channel group. Termination of the group results in the
215
* orderly {@link ExecutorService#shutdown shutdown} of the executor
216
* service. Shutting down the executor service by other means results in
217
* unspecified behavior.
218
*
219
* <p> The group is created by invoking the {@link
220
* AsynchronousChannelProvider#openAsynchronousChannelGroup(ExecutorService,int)
221
* openAsynchronousChannelGroup(ExecutorService,int)} method of the system-wide
222
* default {@link AsynchronousChannelProvider} object.
223
*
224
* @param executor
225
* The thread pool for the resulting group
226
* @param initialSize
227
* A value {@code >=0} or a negative value for implementation
228
* specific default
229
*
230
* @return A new asynchronous channel group
231
*
232
* @throws IOException
233
* If an I/O error occurs
234
*
235
* @see java.util.concurrent.Executors#newCachedThreadPool
236
*/
237
public static AsynchronousChannelGroup withCachedThreadPool(ExecutorService executor,
238
int initialSize)
239
throws IOException
240
{
241
return AsynchronousChannelProvider.provider()
242
.openAsynchronousChannelGroup(executor, initialSize);
243
}
244
245
/**
246
* Creates an asynchronous channel group with a given thread pool.
247
*
248
* <p> The {@code executor} parameter is an {@code ExecutorService} that
249
* executes tasks submitted to dispatch completion results for operations
250
* initiated on asynchronous channels in the group.
251
*
252
* <p> Care should be taken when configuring the executor service. It
253
* should support <em>direct handoff</em> or <em>unbounded queuing</em> of
254
* submitted tasks, and the thread that invokes the {@link
255
* ExecutorService#execute execute} method should never invoke the task
256
* directly. An implementation may mandate additional constraints.
257
*
258
* <p> The executor is intended to be used exclusively by the resulting
259
* asynchronous channel group. Termination of the group results in the
260
* orderly {@link ExecutorService#shutdown shutdown} of the executor
261
* service. Shutting down the executor service by other means results in
262
* unspecified behavior.
263
*
264
* <p> The group is created by invoking the {@link
265
* AsynchronousChannelProvider#openAsynchronousChannelGroup(ExecutorService,int)
266
* openAsynchronousChannelGroup(ExecutorService,int)} method of the system-wide
267
* default {@link AsynchronousChannelProvider} object with an {@code
268
* initialSize} of {@code 0}.
269
*
270
* @param executor
271
* The thread pool for the resulting group
272
*
273
* @return A new asynchronous channel group
274
*
275
* @throws IOException
276
* If an I/O error occurs
277
*/
278
public static AsynchronousChannelGroup withThreadPool(ExecutorService executor)
279
throws IOException
280
{
281
return AsynchronousChannelProvider.provider()
282
.openAsynchronousChannelGroup(executor, 0);
283
}
284
285
/**
286
* Tells whether or not this asynchronous channel group is shutdown.
287
*
288
* @return {@code true} if this asynchronous channel group is shutdown or
289
* has been marked for shutdown.
290
*/
291
public abstract boolean isShutdown();
292
293
/**
294
* Tells whether or not this group has terminated.
295
*
296
* <p> Where this method returns {@code true}, then the associated thread
297
* pool has also {@link ExecutorService#isTerminated terminated}.
298
*
299
* @return {@code true} if this group has terminated
300
*/
301
public abstract boolean isTerminated();
302
303
/**
304
* Initiates an orderly shutdown of the group.
305
*
306
* <p> This method marks the group as shutdown. Further attempts to construct
307
* channel that binds to this group will throw {@link ShutdownChannelGroupException}.
308
* The group terminates when all asynchronous channels in the group are
309
* closed, all actively executing completion handlers have run to completion,
310
* and all resources have been released. This method has no effect if the
311
* group is already shutdown.
312
*/
313
public abstract void shutdown();
314
315
/**
316
* Shuts down the group and closes all open channels in the group.
317
*
318
* <p> In addition to the actions performed by the {@link #shutdown() shutdown}
319
* method, this method invokes the {@link AsynchronousChannel#close close}
320
* method on all open channels in the group. This method does not attempt to
321
* stop or interrupt threads that are executing completion handlers. The
322
* group terminates when all actively executing completion handlers have run
323
* to completion and all resources have been released. This method may be
324
* invoked at any time. If some other thread has already invoked it, then
325
* another invocation will block until the first invocation is complete,
326
* after which it will return without effect.
327
*
328
* @throws IOException
329
* If an I/O error occurs
330
*/
331
public abstract void shutdownNow() throws IOException;
332
333
/**
334
* Awaits termination of the group.
335
*
336
* <p> This method blocks until the group has terminated, or the timeout
337
* occurs, or the current thread is interrupted, whichever happens first.
338
*
339
* @param timeout
340
* The maximum time to wait, or zero or less to not wait
341
* @param unit
342
* The time unit of the timeout argument
343
*
344
* @return {@code true} if the group has terminated; {@code false} if the
345
* timeout elapsed before termination
346
*
347
* @throws InterruptedException
348
* If interrupted while waiting
349
*/
350
public abstract boolean awaitTermination(long timeout, TimeUnit unit)
351
throws InterruptedException;
352
}
353
354