ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jdk7/java/util/concurrent/Executors.java
Revision: 1.5
Committed: Sat Jul 20 20:32:17 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +3 -0 lines
Log Message:
sync javadoc fixes from src/main

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