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