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