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