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