ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Executors.java
Revision: 1.97
Committed: Sat Apr 15 00:12:38 2017 UTC (7 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.96: +1 -0 lines
Log Message:
suppress finalize() warnings

File Contents

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