< prev index next >

## src/java.base/share/classes/java/util/concurrent/Executors.java

Print this page

    372      * within that action.
    373      *
    374      * <p>Note that while tasks running within such threads will have
    375      * the same access control and class loader settings as the
    376      * current thread, they need not have the same {@link
    377      * java.lang.ThreadLocal} or {@link
    378      * java.lang.InheritableThreadLocal} values. If necessary,
    379      * particular values of thread locals can be set or reset before
    380      * any task runs in {@link ThreadPoolExecutor} subclasses using
    381      * {@link ThreadPoolExecutor#beforeExecute(Thread, Runnable)}.
    382      * Also, if it is necessary to initialize worker threads to have
    383      * the same InheritableThreadLocal settings as some other
    384      * designated thread, you can create a custom ThreadFactory in
    385      * which that thread waits for and services requests to create
    386      * others that will inherit its values.
    387      *
    388      * @return a thread factory
    389      * @throws AccessControlException if the current access control
    390      * context does not have permission to both get and set context
    391      * class loader

    392      */

    393     public static ThreadFactory privilegedThreadFactory() {
    394         return new PrivilegedThreadFactory();
    395     }
    396
    397     /**
    398      * Returns a {@link Callable} object that, when
    399      * called, runs the given task and returns the given result.  This
    400      * can be useful when applying methods requiring a
    401      * {@code Callable} to an otherwise resultless action.
    402      * @param task the task to run
    403      * @param result the result to return
    404      * @param <T> the type of the result
    405      * @return a callable object
    406      * @throws NullPointerException if task null
    407      */
    408     public static <T> Callable<T> callable(Runnable task, T result) {
    409         if (task == null)
    410             throw new NullPointerException();
    411         return new RunnableAdapter<T>(task, result);
    412     }
    449     public static Callable<Object> callable(final PrivilegedExceptionAction<?> action) {
    450         if (action == null)
    451             throw new NullPointerException();
    452         return new Callable<Object>() {
    453             public Object call() throws Exception { return action.run(); }};
    454     }
    455
    456     /**
    457      * Returns a {@link Callable} object that will, when called,
    458      * execute the given {@code callable} under the current access
    459      * control context. This method should normally be invoked within
    460      * an {@link AccessController#doPrivileged AccessController.doPrivileged}
    461      * action to create callables that will, if possible, execute
    462      * under the selected permission settings holding within that
    463      * action; or if not possible, throw an associated {@link
    464      * AccessControlException}.
    465      * @param callable the underlying task
    466      * @param <T> the type of the callable's result
    467      * @return a callable object
    468      * @throws NullPointerException if callable null

    469      */

    470     public static <T> Callable<T> privilegedCallable(Callable<T> callable) {
    471         if (callable == null)
    472             throw new NullPointerException();
    473         return new PrivilegedCallable<T>(callable);
    474     }
    475
    476     /**
    477      * Returns a {@link Callable} object that will, when called,
    478      * execute the given {@code callable} under the current access
    479      * control context, with the current context class loader as the
    480      * context class loader. This method should normally be invoked
    481      * within an
    482      * {@link AccessController#doPrivileged AccessController.doPrivileged}
    483      * action to create callables that will, if possible, execute
    484      * under the selected permission settings holding within that
    485      * action; or if not possible, throw an associated {@link
    486      * AccessControlException}.
    487      *
    488      * @param callable the underlying task
    489      * @param <T> the type of the callable's result
    490      * @return a callable object
    491      * @throws NullPointerException if callable null
    492      * @throws AccessControlException if the current access control
    493      * context does not have permission to both set and get context
    494      * class loader

    495      */

    496     public static <T> Callable<T> privilegedCallableUsingCurrentClassLoader(Callable<T> callable) {
    497         if (callable == null)
    498             throw new NullPointerException();
    499         return new PrivilegedCallableUsingCurrentClassLoader<T>(callable);
    500     }
    501
    502     // Non-public classes supporting the public methods
    503
    504     /**
    505      * A callable that runs given task and returns given result.
    506      */
    507     private static final class RunnableAdapter<T> implements Callable<T> {
    508         private final Runnable task;
    509         private final T result;
    510         RunnableAdapter(Runnable task, T result) {
    511             this.task = task;
    512             this.result = result;
    513         }
    514         public T call() {
    515             task.run();
    372      * within that action.
    373      *
    374      * <p>Note that while tasks running within such threads will have
    375      * the same access control and class loader settings as the
    376      * current thread, they need not have the same {@link
    377      * java.lang.ThreadLocal} or {@link
    378      * java.lang.InheritableThreadLocal} values. If necessary,
    379      * particular values of thread locals can be set or reset before
    380      * any task runs in {@link ThreadPoolExecutor} subclasses using
    381      * {@link ThreadPoolExecutor#beforeExecute(Thread, Runnable)}.
    382      * Also, if it is necessary to initialize worker threads to have
    383      * the same InheritableThreadLocal settings as some other
    384      * designated thread, you can create a custom ThreadFactory in
    385      * which that thread waits for and services requests to create
    386      * others that will inherit its values.
    387      *
    388      * @return a thread factory
    389      * @throws AccessControlException if the current access control
    390      * context does not have permission to both get and set context
    391      * class loader
    392      *
    393      * @deprecated This method is only useful in conjunction with
    394      *       {@linkplain SecurityManager the Security Manager}, which is
    395      *       deprecated and subject to removal in a future release.
    396      *       Consequently, this method is also deprecated and subject to
    397      *       removal. There is no replacement for the Security Manager or this
    398      *       method.
    399      */
    400     @Deprecated(since="17", forRemoval=true)
    401     public static ThreadFactory privilegedThreadFactory() {
    402         return new PrivilegedThreadFactory();
    403     }
    404
    405     /**
    406      * Returns a {@link Callable} object that, when
    407      * called, runs the given task and returns the given result.  This
    408      * can be useful when applying methods requiring a
    409      * {@code Callable} to an otherwise resultless action.
    410      * @param task the task to run
    411      * @param result the result to return
    412      * @param <T> the type of the result
    413      * @return a callable object
    414      * @throws NullPointerException if task null
    415      */
    416     public static <T> Callable<T> callable(Runnable task, T result) {
    417         if (task == null)
    418             throw new NullPointerException();
    419         return new RunnableAdapter<T>(task, result);
    420     }
    457     public static Callable<Object> callable(final PrivilegedExceptionAction<?> action) {
    458         if (action == null)
    459             throw new NullPointerException();
    460         return new Callable<Object>() {
    461             public Object call() throws Exception { return action.run(); }};
    462     }
    463
    464     /**
    465      * Returns a {@link Callable} object that will, when called,
    466      * execute the given {@code callable} under the current access
    467      * control context. This method should normally be invoked within
    468      * an {@link AccessController#doPrivileged AccessController.doPrivileged}
    469      * action to create callables that will, if possible, execute
    470      * under the selected permission settings holding within that
    471      * action; or if not possible, throw an associated {@link
    472      * AccessControlException}.
    473      * @param callable the underlying task
    474      * @param <T> the type of the callable's result
    475      * @return a callable object
    476      * @throws NullPointerException if callable null
    477      *
    478      * @deprecated This method is only useful in conjunction with
    479      *       {@linkplain SecurityManager the Security Manager}, which is
    480      *       deprecated and subject to removal in a future release.
    481      *       Consequently, this method is also deprecated and subject to
    482      *       removal. There is no replacement for the Security Manager or this
    483      *       method.
    484      */
    485     @Deprecated(since="17", forRemoval=true)
    486     public static <T> Callable<T> privilegedCallable(Callable<T> callable) {
    487         if (callable == null)
    488             throw new NullPointerException();
    489         return new PrivilegedCallable<T>(callable);
    490     }
    491
    492     /**
    493      * Returns a {@link Callable} object that will, when called,
    494      * execute the given {@code callable} under the current access
    495      * control context, with the current context class loader as the
    496      * context class loader. This method should normally be invoked
    497      * within an
    498      * {@link AccessController#doPrivileged AccessController.doPrivileged}
    499      * action to create callables that will, if possible, execute
    500      * under the selected permission settings holding within that
    501      * action; or if not possible, throw an associated {@link
    502      * AccessControlException}.
    503      *
    504      * @param callable the underlying task
    505      * @param <T> the type of the callable's result
    506      * @return a callable object
    507      * @throws NullPointerException if callable null
    508      * @throws AccessControlException if the current access control
    509      * context does not have permission to both set and get context
    510      * class loader
    511      *
    512      * @deprecated This method is only useful in conjunction with
    513      *       {@linkplain SecurityManager the Security Manager}, which is
    514      *       deprecated and subject to removal in a future release.
    515      *       Consequently, this method is also deprecated and subject to
    516      *       removal. There is no replacement for the Security Manager or this
    517      *       method.
    518      */
    519     @Deprecated(since="17", forRemoval=true)
    520     public static <T> Callable<T> privilegedCallableUsingCurrentClassLoader(Callable<T> callable) {
    521         if (callable == null)
    522             throw new NullPointerException();
    523         return new PrivilegedCallableUsingCurrentClassLoader<T>(callable);
    524     }
    525
    526     // Non-public classes supporting the public methods
    527
    528     /**
    529      * A callable that runs given task and returns given result.
    530      */
    531     private static final class RunnableAdapter<T> implements Callable<T> {
    532         private final Runnable task;
    533         private final T result;
    534         RunnableAdapter(Runnable task, T result) {
    535             this.task = task;
    536             this.result = result;
    537         }
    538         public T call() {
    539             task.run();

< prev index next >