author | dl |
Wed, 21 Dec 2016 14:26:52 -0800 | |
changeset 42927 | 1d31e540bfcb |
parent 42322 | c3474fef4fe4 |
child 44534 | a076dffbc2c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* |
|
26 |
* This file is available under and governed by the GNU General Public |
|
27 |
* License version 2 only, as published by the Free Software Foundation. |
|
28 |
* However, the following notice accompanied the original version of this |
|
29 |
* file: |
|
30 |
* |
|
31 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
32 |
* Expert Group and released to the public domain, as explained at |
|
9242
ef138d47df58
7034657: Update Creative Commons license URL in legal notices
dl
parents:
7518
diff
changeset
|
33 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
2 | 34 |
*/ |
35 |
||
36 |
package java.util.concurrent; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
37 |
|
2 | 38 |
import java.security.AccessControlContext; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
39 |
import java.security.AccessControlException; |
2 | 40 |
import java.security.AccessController; |
41 |
import java.security.PrivilegedAction; |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
42 |
import java.security.PrivilegedActionException; |
2 | 43 |
import java.security.PrivilegedExceptionAction; |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
44 |
import java.util.Collection; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
45 |
import java.util.List; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
46 |
import java.util.concurrent.atomic.AtomicInteger; |
2 | 47 |
import sun.security.util.SecurityConstants; |
48 |
||
49 |
/** |
|
50 |
* Factory and utility methods for {@link Executor}, {@link |
|
51 |
* ExecutorService}, {@link ScheduledExecutorService}, {@link |
|
52 |
* ThreadFactory}, and {@link Callable} classes defined in this |
|
53 |
* package. This class supports the following kinds of methods: |
|
54 |
* |
|
55 |
* <ul> |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
56 |
* <li>Methods that create and return an {@link ExecutorService} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
57 |
* set up with commonly useful configuration settings. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
58 |
* <li>Methods that create and return a {@link ScheduledExecutorService} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
59 |
* set up with commonly useful configuration settings. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
60 |
* <li>Methods that create and return a "wrapped" ExecutorService, that |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
61 |
* disables reconfiguration by making implementation-specific methods |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
62 |
* inaccessible. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
63 |
* <li>Methods that create and return a {@link ThreadFactory} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
64 |
* that sets newly created threads to a known state. |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
65 |
* <li>Methods that create and return a {@link Callable} |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
66 |
* out of other closure-like forms, so they can be used |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
67 |
* in execution methods requiring {@code Callable}. |
2 | 68 |
* </ul> |
69 |
* |
|
70 |
* @since 1.5 |
|
71 |
* @author Doug Lea |
|
72 |
*/ |
|
73 |
public class Executors { |
|
74 |
||
75 |
/** |
|
76 |
* Creates a thread pool that reuses a fixed number of threads |
|
77 |
* operating off a shared unbounded queue. At any point, at most |
|
18790 | 78 |
* {@code nThreads} threads will be active processing tasks. |
2 | 79 |
* If additional tasks are submitted when all threads are active, |
80 |
* they will wait in the queue until a thread is available. |
|
81 |
* If any thread terminates due to a failure during execution |
|
82 |
* prior to shutdown, a new one will take its place if needed to |
|
83 |
* execute subsequent tasks. The threads in the pool will exist |
|
84 |
* until it is explicitly {@link ExecutorService#shutdown shutdown}. |
|
85 |
* |
|
86 |
* @param nThreads the number of threads in the pool |
|
87 |
* @return the newly created thread pool |
|
7518 | 88 |
* @throws IllegalArgumentException if {@code nThreads <= 0} |
2 | 89 |
*/ |
90 |
public static ExecutorService newFixedThreadPool(int nThreads) { |
|
91 |
return new ThreadPoolExecutor(nThreads, nThreads, |
|
92 |
0L, TimeUnit.MILLISECONDS, |
|
93 |
new LinkedBlockingQueue<Runnable>()); |
|
94 |
} |
|
95 |
||
96 |
/** |
|
18790 | 97 |
* Creates a thread pool that maintains enough threads to support |
98 |
* the given parallelism level, and may use multiple queues to |
|
99 |
* reduce contention. The parallelism level corresponds to the |
|
100 |
* maximum number of threads actively engaged in, or available to |
|
101 |
* engage in, task processing. The actual number of threads may |
|
102 |
* grow and shrink dynamically. A work-stealing pool makes no |
|
103 |
* guarantees about the order in which submitted tasks are |
|
104 |
* executed. |
|
105 |
* |
|
106 |
* @param parallelism the targeted parallelism level |
|
107 |
* @return the newly created thread pool |
|
108 |
* @throws IllegalArgumentException if {@code parallelism <= 0} |
|
109 |
* @since 1.8 |
|
110 |
*/ |
|
111 |
public static ExecutorService newWorkStealingPool(int parallelism) { |
|
112 |
return new ForkJoinPool |
|
113 |
(parallelism, |
|
114 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
|
115 |
null, true); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
119 |
* Creates a work-stealing thread pool using the number of |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
120 |
* {@linkplain Runtime#availableProcessors available processors} |
18790 | 121 |
* as its target parallelism level. |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
122 |
* |
18790 | 123 |
* @return the newly created thread pool |
124 |
* @see #newWorkStealingPool(int) |
|
125 |
* @since 1.8 |
|
126 |
*/ |
|
127 |
public static ExecutorService newWorkStealingPool() { |
|
128 |
return new ForkJoinPool |
|
129 |
(Runtime.getRuntime().availableProcessors(), |
|
130 |
ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
|
131 |
null, true); |
|
132 |
} |
|
133 |
||
134 |
/** |
|
2 | 135 |
* Creates a thread pool that reuses a fixed number of threads |
136 |
* operating off a shared unbounded queue, using the provided |
|
137 |
* ThreadFactory to create new threads when needed. At any point, |
|
18790 | 138 |
* at most {@code nThreads} threads will be active processing |
2 | 139 |
* tasks. If additional tasks are submitted when all threads are |
140 |
* active, they will wait in the queue until a thread is |
|
141 |
* available. If any thread terminates due to a failure during |
|
142 |
* execution prior to shutdown, a new one will take its place if |
|
143 |
* needed to execute subsequent tasks. The threads in the pool will |
|
144 |
* exist until it is explicitly {@link ExecutorService#shutdown |
|
145 |
* shutdown}. |
|
146 |
* |
|
147 |
* @param nThreads the number of threads in the pool |
|
148 |
* @param threadFactory the factory to use when creating new threads |
|
149 |
* @return the newly created thread pool |
|
150 |
* @throws NullPointerException if threadFactory is null |
|
7518 | 151 |
* @throws IllegalArgumentException if {@code nThreads <= 0} |
2 | 152 |
*/ |
153 |
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { |
|
154 |
return new ThreadPoolExecutor(nThreads, nThreads, |
|
155 |
0L, TimeUnit.MILLISECONDS, |
|
156 |
new LinkedBlockingQueue<Runnable>(), |
|
157 |
threadFactory); |
|
158 |
} |
|
159 |
||
160 |
/** |
|
161 |
* Creates an Executor that uses a single worker thread operating |
|
162 |
* off an unbounded queue. (Note however that if this single |
|
163 |
* thread terminates due to a failure during execution prior to |
|
164 |
* shutdown, a new one will take its place if needed to execute |
|
165 |
* subsequent tasks.) Tasks are guaranteed to execute |
|
166 |
* sequentially, and no more than one task will be active at any |
|
167 |
* given time. Unlike the otherwise equivalent |
|
18790 | 168 |
* {@code newFixedThreadPool(1)} the returned executor is |
2 | 169 |
* guaranteed not to be reconfigurable to use additional threads. |
170 |
* |
|
171 |
* @return the newly created single-threaded Executor |
|
172 |
*/ |
|
173 |
public static ExecutorService newSingleThreadExecutor() { |
|
174 |
return new FinalizableDelegatedExecutorService |
|
175 |
(new ThreadPoolExecutor(1, 1, |
|
176 |
0L, TimeUnit.MILLISECONDS, |
|
177 |
new LinkedBlockingQueue<Runnable>())); |
|
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Creates an Executor that uses a single worker thread operating |
|
182 |
* off an unbounded queue, and uses the provided ThreadFactory to |
|
183 |
* create a new thread when needed. Unlike the otherwise |
|
18790 | 184 |
* equivalent {@code newFixedThreadPool(1, threadFactory)} the |
2 | 185 |
* returned executor is guaranteed not to be reconfigurable to use |
186 |
* additional threads. |
|
187 |
* |
|
188 |
* @param threadFactory the factory to use when creating new |
|
189 |
* threads |
|
190 |
* |
|
191 |
* @return the newly created single-threaded Executor |
|
192 |
* @throws NullPointerException if threadFactory is null |
|
193 |
*/ |
|
194 |
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) { |
|
195 |
return new FinalizableDelegatedExecutorService |
|
196 |
(new ThreadPoolExecutor(1, 1, |
|
197 |
0L, TimeUnit.MILLISECONDS, |
|
198 |
new LinkedBlockingQueue<Runnable>(), |
|
199 |
threadFactory)); |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Creates a thread pool that creates new threads as needed, but |
|
204 |
* will reuse previously constructed threads when they are |
|
205 |
* available. These pools will typically improve the performance |
|
206 |
* of programs that execute many short-lived asynchronous tasks. |
|
18790 | 207 |
* Calls to {@code execute} will reuse previously constructed |
2 | 208 |
* threads if available. If no existing thread is available, a new |
209 |
* thread will be created and added to the pool. Threads that have |
|
210 |
* not been used for sixty seconds are terminated and removed from |
|
211 |
* the cache. Thus, a pool that remains idle for long enough will |
|
212 |
* not consume any resources. Note that pools with similar |
|
213 |
* properties but different details (for example, timeout parameters) |
|
214 |
* may be created using {@link ThreadPoolExecutor} constructors. |
|
215 |
* |
|
216 |
* @return the newly created thread pool |
|
217 |
*/ |
|
218 |
public static ExecutorService newCachedThreadPool() { |
|
219 |
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
|
220 |
60L, TimeUnit.SECONDS, |
|
221 |
new SynchronousQueue<Runnable>()); |
|
222 |
} |
|
223 |
||
224 |
/** |
|
225 |
* Creates a thread pool that creates new threads as needed, but |
|
226 |
* will reuse previously constructed threads when they are |
|
227 |
* available, and uses the provided |
|
228 |
* ThreadFactory to create new threads when needed. |
|
229 |
* @param threadFactory the factory to use when creating new threads |
|
230 |
* @return the newly created thread pool |
|
231 |
* @throws NullPointerException if threadFactory is null |
|
232 |
*/ |
|
233 |
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) { |
|
234 |
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, |
|
235 |
60L, TimeUnit.SECONDS, |
|
236 |
new SynchronousQueue<Runnable>(), |
|
237 |
threadFactory); |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Creates a single-threaded executor that can schedule commands |
|
242 |
* to run after a given delay, or to execute periodically. |
|
243 |
* (Note however that if this single |
|
244 |
* thread terminates due to a failure during execution prior to |
|
245 |
* shutdown, a new one will take its place if needed to execute |
|
246 |
* subsequent tasks.) Tasks are guaranteed to execute |
|
247 |
* sequentially, and no more than one task will be active at any |
|
248 |
* given time. Unlike the otherwise equivalent |
|
18790 | 249 |
* {@code newScheduledThreadPool(1)} the returned executor is |
2 | 250 |
* guaranteed not to be reconfigurable to use additional threads. |
251 |
* @return the newly created scheduled executor |
|
252 |
*/ |
|
253 |
public static ScheduledExecutorService newSingleThreadScheduledExecutor() { |
|
254 |
return new DelegatedScheduledExecutorService |
|
255 |
(new ScheduledThreadPoolExecutor(1)); |
|
256 |
} |
|
257 |
||
258 |
/** |
|
259 |
* Creates a single-threaded executor that can schedule commands |
|
260 |
* to run after a given delay, or to execute periodically. (Note |
|
261 |
* however that if this single thread terminates due to a failure |
|
262 |
* during execution prior to shutdown, a new one will take its |
|
263 |
* place if needed to execute subsequent tasks.) Tasks are |
|
264 |
* guaranteed to execute sequentially, and no more than one task |
|
265 |
* will be active at any given time. Unlike the otherwise |
|
18790 | 266 |
* equivalent {@code newScheduledThreadPool(1, threadFactory)} |
2 | 267 |
* the returned executor is guaranteed not to be reconfigurable to |
268 |
* use additional threads. |
|
269 |
* @param threadFactory the factory to use when creating new |
|
270 |
* threads |
|
271 |
* @return a newly created scheduled executor |
|
272 |
* @throws NullPointerException if threadFactory is null |
|
273 |
*/ |
|
274 |
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) { |
|
275 |
return new DelegatedScheduledExecutorService |
|
276 |
(new ScheduledThreadPoolExecutor(1, threadFactory)); |
|
277 |
} |
|
278 |
||
279 |
/** |
|
280 |
* Creates a thread pool that can schedule commands to run after a |
|
281 |
* given delay, or to execute periodically. |
|
282 |
* @param corePoolSize the number of threads to keep in the pool, |
|
18790 | 283 |
* even if they are idle |
2 | 284 |
* @return a newly created scheduled thread pool |
7518 | 285 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
2 | 286 |
*/ |
287 |
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { |
|
288 |
return new ScheduledThreadPoolExecutor(corePoolSize); |
|
289 |
} |
|
290 |
||
291 |
/** |
|
292 |
* Creates a thread pool that can schedule commands to run after a |
|
293 |
* given delay, or to execute periodically. |
|
294 |
* @param corePoolSize the number of threads to keep in the pool, |
|
18790 | 295 |
* even if they are idle |
2 | 296 |
* @param threadFactory the factory to use when the executor |
18790 | 297 |
* creates a new thread |
2 | 298 |
* @return a newly created scheduled thread pool |
7518 | 299 |
* @throws IllegalArgumentException if {@code corePoolSize < 0} |
2 | 300 |
* @throws NullPointerException if threadFactory is null |
301 |
*/ |
|
302 |
public static ScheduledExecutorService newScheduledThreadPool( |
|
303 |
int corePoolSize, ThreadFactory threadFactory) { |
|
304 |
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); |
|
305 |
} |
|
306 |
||
307 |
/** |
|
308 |
* Returns an object that delegates all defined {@link |
|
309 |
* ExecutorService} methods to the given executor, but not any |
|
310 |
* other methods that might otherwise be accessible using |
|
311 |
* casts. This provides a way to safely "freeze" configuration and |
|
312 |
* disallow tuning of a given concrete implementation. |
|
313 |
* @param executor the underlying implementation |
|
18790 | 314 |
* @return an {@code ExecutorService} instance |
2 | 315 |
* @throws NullPointerException if executor null |
316 |
*/ |
|
317 |
public static ExecutorService unconfigurableExecutorService(ExecutorService executor) { |
|
318 |
if (executor == null) |
|
319 |
throw new NullPointerException(); |
|
320 |
return new DelegatedExecutorService(executor); |
|
321 |
} |
|
322 |
||
323 |
/** |
|
324 |
* Returns an object that delegates all defined {@link |
|
325 |
* ScheduledExecutorService} methods to the given executor, but |
|
326 |
* not any other methods that might otherwise be accessible using |
|
327 |
* casts. This provides a way to safely "freeze" configuration and |
|
328 |
* disallow tuning of a given concrete implementation. |
|
329 |
* @param executor the underlying implementation |
|
18790 | 330 |
* @return a {@code ScheduledExecutorService} instance |
2 | 331 |
* @throws NullPointerException if executor null |
332 |
*/ |
|
333 |
public static ScheduledExecutorService unconfigurableScheduledExecutorService(ScheduledExecutorService executor) { |
|
334 |
if (executor == null) |
|
335 |
throw new NullPointerException(); |
|
336 |
return new DelegatedScheduledExecutorService(executor); |
|
337 |
} |
|
338 |
||
339 |
/** |
|
340 |
* Returns a default thread factory used to create new threads. |
|
341 |
* This factory creates all new threads used by an Executor in the |
|
342 |
* same {@link ThreadGroup}. If there is a {@link |
|
343 |
* java.lang.SecurityManager}, it uses the group of {@link |
|
344 |
* System#getSecurityManager}, else the group of the thread |
|
18790 | 345 |
* invoking this {@code defaultThreadFactory} method. Each new |
2 | 346 |
* thread is created as a non-daemon thread with priority set to |
18790 | 347 |
* the smaller of {@code Thread.NORM_PRIORITY} and the maximum |
2 | 348 |
* priority permitted in the thread group. New threads have names |
349 |
* accessible via {@link Thread#getName} of |
|
350 |
* <em>pool-N-thread-M</em>, where <em>N</em> is the sequence |
|
351 |
* number of this factory, and <em>M</em> is the sequence number |
|
352 |
* of the thread created by this factory. |
|
353 |
* @return a thread factory |
|
354 |
*/ |
|
355 |
public static ThreadFactory defaultThreadFactory() { |
|
356 |
return new DefaultThreadFactory(); |
|
357 |
} |
|
358 |
||
359 |
/** |
|
360 |
* Returns a thread factory used to create new threads that |
|
361 |
* have the same permissions as the current thread. |
|
362 |
* This factory creates threads with the same settings as {@link |
|
363 |
* Executors#defaultThreadFactory}, additionally setting the |
|
364 |
* AccessControlContext and contextClassLoader of new threads to |
|
365 |
* be the same as the thread invoking this |
|
18790 | 366 |
* {@code privilegedThreadFactory} method. A new |
367 |
* {@code privilegedThreadFactory} can be created within an |
|
368 |
* {@link AccessController#doPrivileged AccessController.doPrivileged} |
|
369 |
* action setting the current thread's access control context to |
|
370 |
* create threads with the selected permission settings holding |
|
371 |
* within that action. |
|
2 | 372 |
* |
18790 | 373 |
* <p>Note that while tasks running within such threads will have |
2 | 374 |
* the same access control and class loader settings as the |
375 |
* current thread, they need not have the same {@link |
|
376 |
* java.lang.ThreadLocal} or {@link |
|
377 |
* java.lang.InheritableThreadLocal} values. If necessary, |
|
378 |
* particular values of thread locals can be set or reset before |
|
379 |
* any task runs in {@link ThreadPoolExecutor} subclasses using |
|
18790 | 380 |
* {@link ThreadPoolExecutor#beforeExecute(Thread, Runnable)}. |
381 |
* Also, if it is necessary to initialize worker threads to have |
|
382 |
* the same InheritableThreadLocal settings as some other |
|
383 |
* designated thread, you can create a custom ThreadFactory in |
|
384 |
* which that thread waits for and services requests to create |
|
385 |
* others that will inherit its values. |
|
2 | 386 |
* |
387 |
* @return a thread factory |
|
388 |
* @throws AccessControlException if the current access control |
|
389 |
* context does not have permission to both get and set context |
|
18790 | 390 |
* class loader |
2 | 391 |
*/ |
392 |
public static ThreadFactory privilegedThreadFactory() { |
|
393 |
return new PrivilegedThreadFactory(); |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Returns a {@link Callable} object that, when |
|
398 |
* called, runs the given task and returns the given result. This |
|
399 |
* can be useful when applying methods requiring a |
|
18790 | 400 |
* {@code Callable} to an otherwise resultless action. |
2 | 401 |
* @param task the task to run |
402 |
* @param result the result to return |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18790
diff
changeset
|
403 |
* @param <T> the type of the result |
2 | 404 |
* @return a callable object |
405 |
* @throws NullPointerException if task null |
|
406 |
*/ |
|
407 |
public static <T> Callable<T> callable(Runnable task, T result) { |
|
408 |
if (task == null) |
|
409 |
throw new NullPointerException(); |
|
410 |
return new RunnableAdapter<T>(task, result); |
|
411 |
} |
|
412 |
||
413 |
/** |
|
414 |
* Returns a {@link Callable} object that, when |
|
18790 | 415 |
* called, runs the given task and returns {@code null}. |
2 | 416 |
* @param task the task to run |
417 |
* @return a callable object |
|
418 |
* @throws NullPointerException if task null |
|
419 |
*/ |
|
420 |
public static Callable<Object> callable(Runnable task) { |
|
421 |
if (task == null) |
|
422 |
throw new NullPointerException(); |
|
423 |
return new RunnableAdapter<Object>(task, null); |
|
424 |
} |
|
425 |
||
426 |
/** |
|
427 |
* Returns a {@link Callable} object that, when |
|
428 |
* called, runs the given privileged action and returns its result. |
|
429 |
* @param action the privileged action to run |
|
430 |
* @return a callable object |
|
431 |
* @throws NullPointerException if action null |
|
432 |
*/ |
|
433 |
public static Callable<Object> callable(final PrivilegedAction<?> action) { |
|
434 |
if (action == null) |
|
435 |
throw new NullPointerException(); |
|
436 |
return new Callable<Object>() { |
|
437 |
public Object call() { return action.run(); }}; |
|
438 |
} |
|
439 |
||
440 |
/** |
|
441 |
* Returns a {@link Callable} object that, when |
|
442 |
* called, runs the given privileged exception action and returns |
|
443 |
* its result. |
|
444 |
* @param action the privileged exception action to run |
|
445 |
* @return a callable object |
|
446 |
* @throws NullPointerException if action null |
|
447 |
*/ |
|
448 |
public static Callable<Object> callable(final PrivilegedExceptionAction<?> action) { |
|
449 |
if (action == null) |
|
450 |
throw new NullPointerException(); |
|
451 |
return new Callable<Object>() { |
|
452 |
public Object call() throws Exception { return action.run(); }}; |
|
453 |
} |
|
454 |
||
455 |
/** |
|
18790 | 456 |
* Returns a {@link Callable} object that will, when called, |
457 |
* execute the given {@code callable} under the current access |
|
458 |
* control context. This method should normally be invoked within |
|
459 |
* an {@link AccessController#doPrivileged AccessController.doPrivileged} |
|
460 |
* action to create callables that will, if possible, execute |
|
461 |
* under the selected permission settings holding within that |
|
462 |
* action; or if not possible, throw an associated {@link |
|
2 | 463 |
* AccessControlException}. |
464 |
* @param callable the underlying task |
|
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18790
diff
changeset
|
465 |
* @param <T> the type of the callable's result |
2 | 466 |
* @return a callable object |
467 |
* @throws NullPointerException if callable null |
|
468 |
*/ |
|
469 |
public static <T> Callable<T> privilegedCallable(Callable<T> callable) { |
|
470 |
if (callable == null) |
|
471 |
throw new NullPointerException(); |
|
472 |
return new PrivilegedCallable<T>(callable); |
|
473 |
} |
|
474 |
||
475 |
/** |
|
18790 | 476 |
* Returns a {@link Callable} object that will, when called, |
477 |
* execute the given {@code callable} under the current access |
|
478 |
* control context, with the current context class loader as the |
|
479 |
* context class loader. This method should normally be invoked |
|
480 |
* within an |
|
481 |
* {@link AccessController#doPrivileged AccessController.doPrivileged} |
|
482 |
* action to create callables that will, if possible, execute |
|
483 |
* under the selected permission settings holding within that |
|
484 |
* action; or if not possible, throw an associated {@link |
|
2 | 485 |
* AccessControlException}. |
18790 | 486 |
* |
2 | 487 |
* @param callable the underlying task |
19048
7d0a94c79779
8021417: Fix doclint issues in java.util.concurrent
chegar
parents:
18790
diff
changeset
|
488 |
* @param <T> the type of the callable's result |
2 | 489 |
* @return a callable object |
490 |
* @throws NullPointerException if callable null |
|
491 |
* @throws AccessControlException if the current access control |
|
492 |
* context does not have permission to both set and get context |
|
18790 | 493 |
* class loader |
2 | 494 |
*/ |
495 |
public static <T> Callable<T> privilegedCallableUsingCurrentClassLoader(Callable<T> callable) { |
|
496 |
if (callable == null) |
|
497 |
throw new NullPointerException(); |
|
498 |
return new PrivilegedCallableUsingCurrentClassLoader<T>(callable); |
|
499 |
} |
|
500 |
||
501 |
// Non-public classes supporting the public methods |
|
502 |
||
503 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
504 |
* A callable that runs given task and returns given result. |
2 | 505 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
506 |
private static final class RunnableAdapter<T> implements Callable<T> { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
507 |
private final Runnable task; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
508 |
private final T result; |
2 | 509 |
RunnableAdapter(Runnable task, T result) { |
510 |
this.task = task; |
|
511 |
this.result = result; |
|
512 |
} |
|
513 |
public T call() { |
|
514 |
task.run(); |
|
515 |
return result; |
|
516 |
} |
|
517 |
} |
|
518 |
||
519 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
520 |
* A callable that runs under established access control settings. |
2 | 521 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
522 |
private static final class PrivilegedCallable<T> implements Callable<T> { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
523 |
final Callable<T> task; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
524 |
final AccessControlContext acc; |
2 | 525 |
|
526 |
PrivilegedCallable(Callable<T> task) { |
|
527 |
this.task = task; |
|
528 |
this.acc = AccessController.getContext(); |
|
529 |
} |
|
530 |
||
531 |
public T call() throws Exception { |
|
532 |
try { |
|
533 |
return AccessController.doPrivileged( |
|
534 |
new PrivilegedExceptionAction<T>() { |
|
535 |
public T run() throws Exception { |
|
536 |
return task.call(); |
|
537 |
} |
|
538 |
}, acc); |
|
539 |
} catch (PrivilegedActionException e) { |
|
540 |
throw e.getException(); |
|
541 |
} |
|
542 |
} |
|
543 |
} |
|
544 |
||
545 |
/** |
|
546 |
* A callable that runs under established access control settings and |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
547 |
* current ClassLoader. |
2 | 548 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
549 |
private static final class PrivilegedCallableUsingCurrentClassLoader<T> |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
550 |
implements Callable<T> { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
551 |
final Callable<T> task; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
552 |
final AccessControlContext acc; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
553 |
final ClassLoader ccl; |
2 | 554 |
|
555 |
PrivilegedCallableUsingCurrentClassLoader(Callable<T> task) { |
|
556 |
SecurityManager sm = System.getSecurityManager(); |
|
557 |
if (sm != null) { |
|
558 |
// Calls to getContextClassLoader from this class |
|
559 |
// never trigger a security check, but we check |
|
560 |
// whether our callers have this permission anyways. |
|
561 |
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
|
562 |
||
563 |
// Whether setContextClassLoader turns out to be necessary |
|
564 |
// or not, we fail fast if permission is not available. |
|
565 |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
|
566 |
} |
|
567 |
this.task = task; |
|
568 |
this.acc = AccessController.getContext(); |
|
569 |
this.ccl = Thread.currentThread().getContextClassLoader(); |
|
570 |
} |
|
571 |
||
572 |
public T call() throws Exception { |
|
573 |
try { |
|
574 |
return AccessController.doPrivileged( |
|
575 |
new PrivilegedExceptionAction<T>() { |
|
576 |
public T run() throws Exception { |
|
577 |
Thread t = Thread.currentThread(); |
|
14214 | 578 |
ClassLoader cl = t.getContextClassLoader(); |
579 |
if (ccl == cl) { |
|
580 |
return task.call(); |
|
581 |
} else { |
|
582 |
t.setContextClassLoader(ccl); |
|
583 |
try { |
|
584 |
return task.call(); |
|
585 |
} finally { |
|
586 |
t.setContextClassLoader(cl); |
|
2 | 587 |
} |
588 |
} |
|
589 |
} |
|
590 |
}, acc); |
|
591 |
} catch (PrivilegedActionException e) { |
|
592 |
throw e.getException(); |
|
593 |
} |
|
594 |
} |
|
595 |
} |
|
596 |
||
597 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
598 |
* The default thread factory. |
2 | 599 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
600 |
private static class DefaultThreadFactory implements ThreadFactory { |
2 | 601 |
private static final AtomicInteger poolNumber = new AtomicInteger(1); |
602 |
private final ThreadGroup group; |
|
603 |
private final AtomicInteger threadNumber = new AtomicInteger(1); |
|
604 |
private final String namePrefix; |
|
605 |
||
606 |
DefaultThreadFactory() { |
|
607 |
SecurityManager s = System.getSecurityManager(); |
|
7518 | 608 |
group = (s != null) ? s.getThreadGroup() : |
609 |
Thread.currentThread().getThreadGroup(); |
|
2 | 610 |
namePrefix = "pool-" + |
611 |
poolNumber.getAndIncrement() + |
|
612 |
"-thread-"; |
|
613 |
} |
|
614 |
||
615 |
public Thread newThread(Runnable r) { |
|
616 |
Thread t = new Thread(group, r, |
|
617 |
namePrefix + threadNumber.getAndIncrement(), |
|
618 |
0); |
|
619 |
if (t.isDaemon()) |
|
620 |
t.setDaemon(false); |
|
621 |
if (t.getPriority() != Thread.NORM_PRIORITY) |
|
622 |
t.setPriority(Thread.NORM_PRIORITY); |
|
623 |
return t; |
|
624 |
} |
|
625 |
} |
|
626 |
||
627 |
/** |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
628 |
* Thread factory capturing access control context and class loader. |
2 | 629 |
*/ |
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
630 |
private static class PrivilegedThreadFactory extends DefaultThreadFactory { |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
631 |
final AccessControlContext acc; |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
632 |
final ClassLoader ccl; |
2 | 633 |
|
634 |
PrivilegedThreadFactory() { |
|
635 |
super(); |
|
636 |
SecurityManager sm = System.getSecurityManager(); |
|
637 |
if (sm != null) { |
|
638 |
// Calls to getContextClassLoader from this class |
|
639 |
// never trigger a security check, but we check |
|
640 |
// whether our callers have this permission anyways. |
|
641 |
sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION); |
|
642 |
||
643 |
// Fail fast |
|
644 |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
|
645 |
} |
|
646 |
this.acc = AccessController.getContext(); |
|
647 |
this.ccl = Thread.currentThread().getContextClassLoader(); |
|
648 |
} |
|
649 |
||
650 |
public Thread newThread(final Runnable r) { |
|
651 |
return super.newThread(new Runnable() { |
|
652 |
public void run() { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
32991
diff
changeset
|
653 |
AccessController.doPrivileged(new PrivilegedAction<>() { |
2 | 654 |
public Void run() { |
655 |
Thread.currentThread().setContextClassLoader(ccl); |
|
656 |
r.run(); |
|
657 |
return null; |
|
658 |
} |
|
659 |
}, acc); |
|
660 |
} |
|
661 |
}); |
|
662 |
} |
|
663 |
} |
|
664 |
||
665 |
/** |
|
666 |
* A wrapper class that exposes only the ExecutorService methods |
|
667 |
* of an ExecutorService implementation. |
|
668 |
*/ |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
669 |
private static class DelegatedExecutorService |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
670 |
extends AbstractExecutorService { |
2 | 671 |
private final ExecutorService e; |
672 |
DelegatedExecutorService(ExecutorService executor) { e = executor; } |
|
673 |
public void execute(Runnable command) { e.execute(command); } |
|
674 |
public void shutdown() { e.shutdown(); } |
|
675 |
public List<Runnable> shutdownNow() { return e.shutdownNow(); } |
|
676 |
public boolean isShutdown() { return e.isShutdown(); } |
|
677 |
public boolean isTerminated() { return e.isTerminated(); } |
|
678 |
public boolean awaitTermination(long timeout, TimeUnit unit) |
|
679 |
throws InterruptedException { |
|
680 |
return e.awaitTermination(timeout, unit); |
|
681 |
} |
|
682 |
public Future<?> submit(Runnable task) { |
|
683 |
return e.submit(task); |
|
684 |
} |
|
685 |
public <T> Future<T> submit(Callable<T> task) { |
|
686 |
return e.submit(task); |
|
687 |
} |
|
688 |
public <T> Future<T> submit(Runnable task, T result) { |
|
689 |
return e.submit(task, result); |
|
690 |
} |
|
691 |
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) |
|
692 |
throws InterruptedException { |
|
693 |
return e.invokeAll(tasks); |
|
694 |
} |
|
695 |
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, |
|
696 |
long timeout, TimeUnit unit) |
|
697 |
throws InterruptedException { |
|
698 |
return e.invokeAll(tasks, timeout, unit); |
|
699 |
} |
|
700 |
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) |
|
701 |
throws InterruptedException, ExecutionException { |
|
702 |
return e.invokeAny(tasks); |
|
703 |
} |
|
704 |
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, |
|
705 |
long timeout, TimeUnit unit) |
|
706 |
throws InterruptedException, ExecutionException, TimeoutException { |
|
707 |
return e.invokeAny(tasks, timeout, unit); |
|
708 |
} |
|
709 |
} |
|
710 |
||
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
711 |
private static class FinalizableDelegatedExecutorService |
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
712 |
extends DelegatedExecutorService { |
2 | 713 |
FinalizableDelegatedExecutorService(ExecutorService executor) { |
714 |
super(executor); |
|
715 |
} |
|
7518 | 716 |
protected void finalize() { |
2 | 717 |
super.shutdown(); |
718 |
} |
|
719 |
} |
|
720 |
||
721 |
/** |
|
722 |
* A wrapper class that exposes only the ScheduledExecutorService |
|
723 |
* methods of a ScheduledExecutorService implementation. |
|
724 |
*/ |
|
32991
b27c76b82713
8134853: Bulk integration of java.util.concurrent classes
dl
parents:
25859
diff
changeset
|
725 |
private static class DelegatedScheduledExecutorService |
2 | 726 |
extends DelegatedExecutorService |
727 |
implements ScheduledExecutorService { |
|
728 |
private final ScheduledExecutorService e; |
|
729 |
DelegatedScheduledExecutorService(ScheduledExecutorService executor) { |
|
730 |
super(executor); |
|
731 |
e = executor; |
|
732 |
} |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
733 |
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) { |
2 | 734 |
return e.schedule(command, delay, unit); |
735 |
} |
|
736 |
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) { |
|
737 |
return e.schedule(callable, delay, unit); |
|
738 |
} |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
739 |
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { |
2 | 740 |
return e.scheduleAtFixedRate(command, initialDelay, period, unit); |
741 |
} |
|
14325
622c473a21aa
8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents:
9242
diff
changeset
|
742 |
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { |
2 | 743 |
return e.scheduleWithFixedDelay(command, initialDelay, delay, unit); |
744 |
} |
|
745 |
} |
|
746 |
||
747 |
/** Cannot instantiate. */ |
|
748 |
private Executors() {} |
|
749 |
} |