jdk/src/share/classes/java/util/concurrent/ExecutorService.java
author martin
Mon, 10 Mar 2008 23:23:47 -0700
changeset 61 5691b03db1ea
parent 2 90ce3da70b43
child 4110 ac033ba6ede4
permissions -rw-r--r--
6620549: ExecutorService#shutdown should clearly state that it does not block Reviewed-by: dholmes Contributed-by: Doug Lea <dl@cs.oswego.edu>
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * have any questions.
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.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * An {@link Executor} that provides methods to manage termination and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * methods that can produce a {@link Future} for tracking progress of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * one or more asynchronous tasks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> An <tt>ExecutorService</tt> can be shut down, which will cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * it to reject new tasks.  Two different methods are provided for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * shutting down an <tt>ExecutorService</tt>. The {@link #shutdown}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * method will allow previously submitted tasks to execute before
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * terminating, while the {@link #shutdownNow} method prevents waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * tasks from starting and attempts to stop currently executing tasks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Upon termination, an executor has no tasks actively executing, no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * tasks awaiting execution, and no new tasks can be submitted.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * unused <tt>ExecutorService</tt> should be shut down to allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * reclamation of its resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p> Method <tt>submit</tt> extends base method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Executor#execute} by creating and returning a {@link Future} that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * can be used to cancel execution and/or wait for completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Methods <tt>invokeAny</tt> and <tt>invokeAll</tt> perform the most
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * commonly useful forms of bulk execution, executing a collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * tasks and then waiting for at least one, or all, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * complete. (Class {@link ExecutorCompletionService} can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * write customized variants of these methods.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>The {@link Executors} class provides factory methods for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * executor services provided in this package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <h3>Usage Examples</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Here is a sketch of a network service in which threads in a thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * pool service incoming requests. It uses the preconfigured {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * Executors#newFixedThreadPool} factory method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * class NetworkService implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *   private final ServerSocket serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *   private final ExecutorService pool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *   public NetworkService(int port, int poolSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *       throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *     serverSocket = new ServerSocket(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *     pool = Executors.newFixedThreadPool(poolSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *   public void run() { // run the service
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *       for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *         pool.execute(new Handler(serverSocket.accept()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *     } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *       pool.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * class Handler implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   private final Socket socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   Handler(Socket socket) { this.socket = socket; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *     // read and service request on socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * The following method shuts down an <tt>ExecutorService</tt> in two phases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * first by calling <tt>shutdown</tt> to reject incoming tasks, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * calling <tt>shutdownNow</tt>, if necessary, to cancel any lingering tasks:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * void shutdownAndAwaitTermination(ExecutorService pool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *   pool.shutdown(); // Disable new tasks from being submitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *   try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *     // Wait a while for existing tasks to terminate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *       pool.shutdownNow(); // Cancel currently executing tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *       // Wait a while for tasks to respond to being cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *       if (!pool.awaitTermination(60, TimeUnit.SECONDS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *           System.err.println("Pool did not terminate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *   } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *     // (Re-)Cancel if current thread also interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *     pool.shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 *     // Preserve interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *     Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * <p>Memory consistency effects: Actions in a thread prior to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * submission of a {@code Runnable} or {@code Callable} task to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * {@code ExecutorService}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * any actions taken by that task, which in turn <i>happen-before</i> the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * result is retrieved via {@code Future.get()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
public interface ExecutorService extends Executor {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Initiates an orderly shutdown in which previously submitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * tasks are executed, but no new tasks will be accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Invocation has no additional effect if already shut down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
61
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   148
     * <p>This method does not wait for previously submitted tasks to
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   149
     * complete execution.  Use {@link #awaitTermination awaitTermination}
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   150
     * to do that.
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   151
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws SecurityException if a security manager exists and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *         shutting down this ExecutorService may manipulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         threads that the caller is not permitted to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         because it does not hold {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *         java.lang.RuntimePermission}<tt>("modifyThread")</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *         or the security manager's <tt>checkAccess</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *         denies access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    void shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Attempts to stop all actively executing tasks, halts the
61
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   164
     * processing of waiting tasks, and returns a list of the tasks
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   165
     * that were awaiting execution.
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   166
     *
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   167
     * <p>This method does not wait for actively executing tasks to
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   168
     * terminate.  Use {@link #awaitTermination awaitTermination} to
5691b03db1ea 6620549: ExecutorService#shutdown should clearly state that it does not block
martin
parents: 2
diff changeset
   169
     * do that.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p>There are no guarantees beyond best-effort attempts to stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * processing actively executing tasks.  For example, typical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * implementations will cancel via {@link Thread#interrupt}, so any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * task that fails to respond to interrupts may never terminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return list of tasks that never commenced execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @throws SecurityException if a security manager exists and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *         shutting down this ExecutorService may manipulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *         threads that the caller is not permitted to modify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *         because it does not hold {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *         java.lang.RuntimePermission}<tt>("modifyThread")</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *         or the security manager's <tt>checkAccess</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *         denies access.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    List<Runnable> shutdownNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Returns <tt>true</tt> if this executor has been shut down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return <tt>true</tt> if this executor has been shut down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    boolean isShutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns <tt>true</tt> if all tasks have completed following shut down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Note that <tt>isTerminated</tt> is never <tt>true</tt> unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * either <tt>shutdown</tt> or <tt>shutdownNow</tt> was called first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @return <tt>true</tt> if all tasks have completed following shut down
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    boolean isTerminated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Blocks until all tasks have completed execution after a shutdown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * request, or the timeout occurs, or the current thread is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * interrupted, whichever happens first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param timeout the maximum time to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @param unit the time unit of the timeout argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return <tt>true</tt> if this executor terminated and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *         <tt>false</tt> if the timeout elapsed before termination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @throws InterruptedException if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    boolean awaitTermination(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        throws InterruptedException;
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
     * Submits a value-returning task for execution and returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Future representing the pending results of the task. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Future's <tt>get</tt> method will return the task's result upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * successful completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * If you would like to immediately block waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * for a task, you can use constructions of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <tt>result = exec.submit(aCallable).get();</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p> Note: The {@link Executors} class includes a set of methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * that can convert some other common closure-like objects,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * for example, {@link java.security.PrivilegedAction} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * {@link Callable} form so they can be submitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param task the task to submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return a Future representing pending completion of the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @throws RejectedExecutionException if the task cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *         scheduled for execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @throws NullPointerException if the task is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    <T> Future<T> submit(Callable<T> task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * Submits a Runnable task for execution and returns a Future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * representing that task. The Future's <tt>get</tt> method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * return the given result upon successful completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param task the task to submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param result the result to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @return a Future representing pending completion of the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @throws RejectedExecutionException if the task cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *         scheduled for execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @throws NullPointerException if the task is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    <T> Future<T> submit(Runnable task, T result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Submits a Runnable task for execution and returns a Future
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * representing that task. The Future's <tt>get</tt> method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * return <tt>null</tt> upon <em>successful</em> completion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param task the task to submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @return a Future representing pending completion of the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @throws RejectedExecutionException if the task cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *         scheduled for execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @throws NullPointerException if the task is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    Future<?> submit(Runnable task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Executes the given tasks, returning a list of Futures holding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * their status and results when all complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * {@link Future#isDone} is <tt>true</tt> for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * element of the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Note that a <em>completed</em> task could have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * terminated either normally or by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * The results of this method are undefined if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * collection is modified while this operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param tasks the collection of tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return A list of Futures representing the tasks, in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *         sequential order as produced by the iterator for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *         given task list, each of which has completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @throws InterruptedException if interrupted while waiting, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *         which case unfinished tasks are cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws NullPointerException if tasks or any of its elements are <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @throws RejectedExecutionException if any task cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *         scheduled for execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Executes the given tasks, returning a list of Futures holding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * their status and results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * when all complete or the timeout expires, whichever happens first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * {@link Future#isDone} is <tt>true</tt> for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * element of the returned list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Upon return, tasks that have not completed are cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Note that a <em>completed</em> task could have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * terminated either normally or by throwing an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * The results of this method are undefined if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * collection is modified while this operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @param tasks the collection of tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @param timeout the maximum time to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param unit the time unit of the timeout argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @return a list of Futures representing the tasks, in the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *         sequential order as produced by the iterator for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *         given task list. If the operation did not time out,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *         each task will have completed. If it did time out, some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *         of these tasks will not have completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @throws InterruptedException if interrupted while waiting, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *         which case unfinished tasks are cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @throws NullPointerException if tasks, any of its elements, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *         unit are <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * @throws RejectedExecutionException if any task cannot be scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *         for execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                  long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        throws InterruptedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * Executes the given tasks, returning the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * of one that has completed successfully (i.e., without throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * an exception), if any do. Upon normal or exceptional return,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * tasks that have not completed are cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * The results of this method are undefined if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * collection is modified while this operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param tasks the collection of tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @return the result returned by one of the tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @throws InterruptedException if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @throws NullPointerException if tasks or any of its elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *         are <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @throws IllegalArgumentException if tasks is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * @throws ExecutionException if no task successfully completes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @throws RejectedExecutionException if tasks cannot be scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *         for execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    <T> T invokeAny(Collection<? extends Callable<T>> tasks)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        throws InterruptedException, ExecutionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * Executes the given tasks, returning the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * of one that has completed successfully (i.e., without throwing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * an exception), if any do before the given timeout elapses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * Upon normal or exceptional return, tasks that have not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * completed are cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * The results of this method are undefined if the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * collection is modified while this operation is in progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * @param tasks the collection of tasks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param timeout the maximum time to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param unit the time unit of the timeout argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return the result returned by one of the tasks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @throws InterruptedException if interrupted while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws NullPointerException if tasks, any of its elements, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *         unit are <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @throws TimeoutException if the given timeout elapses before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *         any task successfully completes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @throws ExecutionException if no task successfully completes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @throws RejectedExecutionException if tasks cannot be scheduled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *         for execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    <T> T invokeAny(Collection<? extends Callable<T>> tasks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        throws InterruptedException, ExecutionException, TimeoutException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}