jdk/src/share/classes/java/util/concurrent/Future.java
author sherman
Wed, 21 Oct 2009 11:40:40 -0700
changeset 4161 679d00486dc6
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6878475: Better syntax for the named capture group in regex Summary: Updated the syntax of the newly added named capture group Reviewed-by: martin, alanb
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * A <tt>Future</tt> represents the result of an asynchronous
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * computation.  Methods are provided to check if the computation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * complete, to wait for its completion, and to retrieve the result of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the computation.  The result can only be retrieved using method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <tt>get</tt> when the computation has completed, blocking if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * necessary until it is ready.  Cancellation is performed by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <tt>cancel</tt> method.  Additional methods are provided to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * determine if the task completed normally or was cancelled. Once a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * computation has completed, the computation cannot be cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * If you would like to use a <tt>Future</tt> for the sake
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * of cancellability but not provide a usable result, you can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * declare types of the form <tt>Future&lt;?&gt;</tt> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * return <tt>null</tt> as a result of the underlying task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <b>Sample Usage</b> (Note that the following classes are all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * made-up.) <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * interface ArchiveSearcher { String search(String target); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * class App {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   ExecutorService executor = ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   ArchiveSearcher searcher = ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   void showSearch(final String target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *       throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     Future&lt;String&gt; future
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *       = executor.submit(new Callable&lt;String&gt;() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *         public String call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *             return searcher.search(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *         }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *     displayOtherThings(); // do other things while searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *       displayText(future.get()); // use future
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *     } catch (ExecutionException ex) { cleanup(); return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * The {@link FutureTask} class is an implementation of <tt>Future</tt> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * For example, the above construction with <tt>submit</tt> could be replaced by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *     FutureTask&lt;String&gt; future =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *       new FutureTask&lt;String&gt;(new Callable&lt;String&gt;() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *         public String call() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *           return searcher.search(target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *       }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     executor.execute(future);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>Memory consistency effects: Actions taken by the asynchronous computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * actions following the corresponding {@code Future.get()} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * @see FutureTask
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * @see Executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @author Doug Lea
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @param <V> The result type returned by this Future's <tt>get</tt> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
public interface Future<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Attempts to cancel execution of this task.  This attempt will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * fail if the task has already completed, has already been cancelled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * or could not be cancelled for some other reason. If successful,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * and this task has not started when <tt>cancel</tt> is called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * this task should never run.  If the task has already started,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * then the <tt>mayInterruptIfRunning</tt> parameter determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * whether the thread executing this task should be interrupted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * an attempt to stop the task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>After this method returns, subsequent calls to {@link #isDone} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * always return <tt>true</tt>.  Subsequent calls to {@link #isCancelled}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * will always return <tt>true</tt> if this method returned <tt>true</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * task should be interrupted; otherwise, in-progress tasks are allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * to complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return <tt>false</tt> if the task could not be cancelled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * typically because it has already completed normally;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <tt>true</tt> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    boolean cancel(boolean mayInterruptIfRunning);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Returns <tt>true</tt> if this task was cancelled before it completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @return <tt>true</tt> if this task was cancelled before it completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    boolean isCancelled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Returns <tt>true</tt> if this task completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Completion may be due to normal termination, an exception, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * cancellation -- in all of these cases, this method will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <tt>true</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return <tt>true</tt> if this task completed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    boolean isDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Waits if necessary for the computation to complete, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * retrieves its result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @return the computed result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @throws CancellationException if the computation was cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws ExecutionException if the computation threw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws InterruptedException if the current thread was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    V get() throws InterruptedException, ExecutionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Waits if necessary for at most the given time for the computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * to complete, and then retrieves its result, if available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param timeout the maximum time to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param unit the time unit of the timeout argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return the computed result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @throws CancellationException if the computation was cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws ExecutionException if the computation threw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @throws InterruptedException if the current thread was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @throws TimeoutException if the wait timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    V get(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        throws InterruptedException, ExecutionException, TimeoutException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}