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