ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Executors.java
Revision: 1.67
Committed: Thu Apr 20 07:12:49 2006 UTC (18 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.66: +27 -19 lines
Log Message:
6404123: @link => @linkplain and other minor doc fixes; 6398290: Exception specifications in ScheduledThreadPoolExecutor/Executors

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