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