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