ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Executors.java
Revision: 1.61
Committed: Thu Aug 25 19:28:17 2005 UTC (18 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.60: +4 -4 lines
Log Message:
6267833: Incorrect method signature ExecutorService.invokeAll()

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