rt/emul/compact/src/main/java/java/util/concurrent/Future.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 19 Mar 2016 10:46:31 +0100
branchjdk7-b147
changeset 1890 212417b74b72
permissions -rw-r--r--
Bringing in all concurrent package from JDK7-b147
jaroslav@1890
     1
/*
jaroslav@1890
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1890
     3
 *
jaroslav@1890
     4
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1890
     5
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1890
     6
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1890
     7
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1890
     8
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1890
     9
 *
jaroslav@1890
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1890
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1890
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1890
    13
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1890
    14
 * accompanied this code).
jaroslav@1890
    15
 *
jaroslav@1890
    16
 * You should have received a copy of the GNU General Public License version
jaroslav@1890
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1890
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1890
    19
 *
jaroslav@1890
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1890
    21
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1890
    22
 * questions.
jaroslav@1890
    23
 */
jaroslav@1890
    24
jaroslav@1890
    25
/*
jaroslav@1890
    26
 * This file is available under and governed by the GNU General Public
jaroslav@1890
    27
 * License version 2 only, as published by the Free Software Foundation.
jaroslav@1890
    28
 * However, the following notice accompanied the original version of this
jaroslav@1890
    29
 * file:
jaroslav@1890
    30
 *
jaroslav@1890
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
jaroslav@1890
    32
 * Expert Group and released to the public domain, as explained at
jaroslav@1890
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
jaroslav@1890
    34
 */
jaroslav@1890
    35
jaroslav@1890
    36
package java.util.concurrent;
jaroslav@1890
    37
jaroslav@1890
    38
/**
jaroslav@1890
    39
 * A <tt>Future</tt> represents the result of an asynchronous
jaroslav@1890
    40
 * computation.  Methods are provided to check if the computation is
jaroslav@1890
    41
 * complete, to wait for its completion, and to retrieve the result of
jaroslav@1890
    42
 * the computation.  The result can only be retrieved using method
jaroslav@1890
    43
 * <tt>get</tt> when the computation has completed, blocking if
jaroslav@1890
    44
 * necessary until it is ready.  Cancellation is performed by the
jaroslav@1890
    45
 * <tt>cancel</tt> method.  Additional methods are provided to
jaroslav@1890
    46
 * determine if the task completed normally or was cancelled. Once a
jaroslav@1890
    47
 * computation has completed, the computation cannot be cancelled.
jaroslav@1890
    48
 * If you would like to use a <tt>Future</tt> for the sake
jaroslav@1890
    49
 * of cancellability but not provide a usable result, you can
jaroslav@1890
    50
 * declare types of the form {@code Future<?>} and
jaroslav@1890
    51
 * return <tt>null</tt> as a result of the underlying task.
jaroslav@1890
    52
 *
jaroslav@1890
    53
 * <p>
jaroslav@1890
    54
 * <b>Sample Usage</b> (Note that the following classes are all
jaroslav@1890
    55
 * made-up.) <p>
jaroslav@1890
    56
 *  <pre> {@code
jaroslav@1890
    57
 * interface ArchiveSearcher { String search(String target); }
jaroslav@1890
    58
 * class App {
jaroslav@1890
    59
 *   ExecutorService executor = ...
jaroslav@1890
    60
 *   ArchiveSearcher searcher = ...
jaroslav@1890
    61
 *   void showSearch(final String target)
jaroslav@1890
    62
 *       throws InterruptedException {
jaroslav@1890
    63
 *     Future<String> future
jaroslav@1890
    64
 *       = executor.submit(new Callable<String>() {
jaroslav@1890
    65
 *         public String call() {
jaroslav@1890
    66
 *             return searcher.search(target);
jaroslav@1890
    67
 *         }});
jaroslav@1890
    68
 *     displayOtherThings(); // do other things while searching
jaroslav@1890
    69
 *     try {
jaroslav@1890
    70
 *       displayText(future.get()); // use future
jaroslav@1890
    71
 *     } catch (ExecutionException ex) { cleanup(); return; }
jaroslav@1890
    72
 *   }
jaroslav@1890
    73
 * }}</pre>
jaroslav@1890
    74
 *
jaroslav@1890
    75
 * The {@link FutureTask} class is an implementation of <tt>Future</tt> that
jaroslav@1890
    76
 * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>.
jaroslav@1890
    77
 * For example, the above construction with <tt>submit</tt> could be replaced by:
jaroslav@1890
    78
 *  <pre> {@code
jaroslav@1890
    79
 *     FutureTask<String> future =
jaroslav@1890
    80
 *       new FutureTask<String>(new Callable<String>() {
jaroslav@1890
    81
 *         public String call() {
jaroslav@1890
    82
 *           return searcher.search(target);
jaroslav@1890
    83
 *       }});
jaroslav@1890
    84
 *     executor.execute(future);}</pre>
jaroslav@1890
    85
 *
jaroslav@1890
    86
 * <p>Memory consistency effects: Actions taken by the asynchronous computation
jaroslav@1890
    87
 * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
jaroslav@1890
    88
 * actions following the corresponding {@code Future.get()} in another thread.
jaroslav@1890
    89
 *
jaroslav@1890
    90
 * @see FutureTask
jaroslav@1890
    91
 * @see Executor
jaroslav@1890
    92
 * @since 1.5
jaroslav@1890
    93
 * @author Doug Lea
jaroslav@1890
    94
 * @param <V> The result type returned by this Future's <tt>get</tt> method
jaroslav@1890
    95
 */
jaroslav@1890
    96
public interface Future<V> {
jaroslav@1890
    97
jaroslav@1890
    98
    /**
jaroslav@1890
    99
     * Attempts to cancel execution of this task.  This attempt will
jaroslav@1890
   100
     * fail if the task has already completed, has already been cancelled,
jaroslav@1890
   101
     * or could not be cancelled for some other reason. If successful,
jaroslav@1890
   102
     * and this task has not started when <tt>cancel</tt> is called,
jaroslav@1890
   103
     * this task should never run.  If the task has already started,
jaroslav@1890
   104
     * then the <tt>mayInterruptIfRunning</tt> parameter determines
jaroslav@1890
   105
     * whether the thread executing this task should be interrupted in
jaroslav@1890
   106
     * an attempt to stop the task.
jaroslav@1890
   107
     *
jaroslav@1890
   108
     * <p>After this method returns, subsequent calls to {@link #isDone} will
jaroslav@1890
   109
     * always return <tt>true</tt>.  Subsequent calls to {@link #isCancelled}
jaroslav@1890
   110
     * will always return <tt>true</tt> if this method returned <tt>true</tt>.
jaroslav@1890
   111
     *
jaroslav@1890
   112
     * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
jaroslav@1890
   113
     * task should be interrupted; otherwise, in-progress tasks are allowed
jaroslav@1890
   114
     * to complete
jaroslav@1890
   115
     * @return <tt>false</tt> if the task could not be cancelled,
jaroslav@1890
   116
     * typically because it has already completed normally;
jaroslav@1890
   117
     * <tt>true</tt> otherwise
jaroslav@1890
   118
     */
jaroslav@1890
   119
    boolean cancel(boolean mayInterruptIfRunning);
jaroslav@1890
   120
jaroslav@1890
   121
    /**
jaroslav@1890
   122
     * Returns <tt>true</tt> if this task was cancelled before it completed
jaroslav@1890
   123
     * normally.
jaroslav@1890
   124
     *
jaroslav@1890
   125
     * @return <tt>true</tt> if this task was cancelled before it completed
jaroslav@1890
   126
     */
jaroslav@1890
   127
    boolean isCancelled();
jaroslav@1890
   128
jaroslav@1890
   129
    /**
jaroslav@1890
   130
     * Returns <tt>true</tt> if this task completed.
jaroslav@1890
   131
     *
jaroslav@1890
   132
     * Completion may be due to normal termination, an exception, or
jaroslav@1890
   133
     * cancellation -- in all of these cases, this method will return
jaroslav@1890
   134
     * <tt>true</tt>.
jaroslav@1890
   135
     *
jaroslav@1890
   136
     * @return <tt>true</tt> if this task completed
jaroslav@1890
   137
     */
jaroslav@1890
   138
    boolean isDone();
jaroslav@1890
   139
jaroslav@1890
   140
    /**
jaroslav@1890
   141
     * Waits if necessary for the computation to complete, and then
jaroslav@1890
   142
     * retrieves its result.
jaroslav@1890
   143
     *
jaroslav@1890
   144
     * @return the computed result
jaroslav@1890
   145
     * @throws CancellationException if the computation was cancelled
jaroslav@1890
   146
     * @throws ExecutionException if the computation threw an
jaroslav@1890
   147
     * exception
jaroslav@1890
   148
     * @throws InterruptedException if the current thread was interrupted
jaroslav@1890
   149
     * while waiting
jaroslav@1890
   150
     */
jaroslav@1890
   151
    V get() throws InterruptedException, ExecutionException;
jaroslav@1890
   152
jaroslav@1890
   153
    /**
jaroslav@1890
   154
     * Waits if necessary for at most the given time for the computation
jaroslav@1890
   155
     * to complete, and then retrieves its result, if available.
jaroslav@1890
   156
     *
jaroslav@1890
   157
     * @param timeout the maximum time to wait
jaroslav@1890
   158
     * @param unit the time unit of the timeout argument
jaroslav@1890
   159
     * @return the computed result
jaroslav@1890
   160
     * @throws CancellationException if the computation was cancelled
jaroslav@1890
   161
     * @throws ExecutionException if the computation threw an
jaroslav@1890
   162
     * exception
jaroslav@1890
   163
     * @throws InterruptedException if the current thread was interrupted
jaroslav@1890
   164
     * while waiting
jaroslav@1890
   165
     * @throws TimeoutException if the wait timed out
jaroslav@1890
   166
     */
jaroslav@1890
   167
    V get(long timeout, TimeUnit unit)
jaroslav@1890
   168
        throws InterruptedException, ExecutionException, TimeoutException;
jaroslav@1890
   169
}