ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Executors.java
Revision: 1.54
Committed: Wed May 18 01:44:45 2005 UTC (19 years ago) by jsr166
Branch: MAIN
Changes since 1.53: +1 -1 lines
Log Message:
whitespace

File Contents

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