jdk/src/java.base/share/classes/java/util/concurrent/Future.java
author dl
Fri, 03 Mar 2017 10:45:38 -0800
changeset 44039 058585425bb7
parent 42322 c3474fef4fe4
permissions -rw-r--r--
8173909: Miscellaneous changes imported from jsr166 CVS 2017-03 Reviewed-by: martin, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     6
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     8
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This file is available under and governed by the GNU General Public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * License version 2 only, as published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * However, the following notice accompanied the original version of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Written by Doug Lea with assistance from members of JCP JSR-166
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Expert Group and released to the public domain, as explained at
9242
ef138d47df58 7034657: Update Creative Commons license URL in legal notices
dl
parents: 7518
diff changeset
    33
 * http://creativecommons.org/publicdomain/zero/1.0/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
package java.util.concurrent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    39
 * A {@code Future} represents the result of an asynchronous
2
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
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    43
 * {@code get} when the computation has completed, blocking if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * necessary until it is ready.  Cancellation is performed by the
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    45
 * {@code cancel} method.  Additional methods are provided to
2
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.
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    48
 * If you would like to use a {@code Future} for the sake
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * of cancellability but not provide a usable result, you can
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    50
 * declare types of the form {@code Future<?>} and
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    51
 * return {@code null} as a result of the underlying task.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 32991
diff changeset
    53
 * <p><b>Sample Usage</b> (Note that the following classes are all
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18790
diff changeset
    54
 * made-up.)
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    55
 *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18790
diff changeset
    56
 * <pre> {@code
2
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 = ...
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 32991
diff changeset
    61
 *   void showSearch(String target) throws InterruptedException {
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 32991
diff changeset
    62
 *     Callable<String> task = () -> searcher.search(target);
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 32991
diff changeset
    63
 *     Future<String> future = executor.submit(task);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *     displayOtherThings(); // do other things while searching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *     try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *       displayText(future.get()); // use future
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     } catch (ExecutionException ex) { cleanup(); return; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *   }
7518
0282db800fe1 7003745: Code style cleanups (sync from Dougs CVS)
dl
parents: 5506
diff changeset
    69
 * }}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    71
 * The {@link FutureTask} class is an implementation of {@code Future} that
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    72
 * implements {@code Runnable}, and so may be executed by an {@code Executor}.
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    73
 * For example, the above construction with {@code submit} could be replaced by:
32991
b27c76b82713 8134853: Bulk integration of java.util.concurrent classes
dl
parents: 25859
diff changeset
    74
 * <pre> {@code
42322
c3474fef4fe4 8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents: 32991
diff changeset
    75
 * FutureTask<String> future = new FutureTask<>(task);
14325
622c473a21aa 8001575: Minor/sync/cleanup j.u.c with Dougs CVS - Oct 2012
dl
parents: 9242
diff changeset
    76
 * executor.execute(future);}</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>Memory consistency effects: Actions taken by the asynchronous computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * actions following the corresponding {@code Future.get()} in another thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * @see FutureTask
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @see Executor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @author Doug Lea
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    86
 * @param <V> The result type returned by this Future's {@code get} method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
public interface Future<V> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Attempts to cancel execution of this task.  This attempt will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * fail if the task has already completed, has already been cancelled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * or could not be cancelled for some other reason. If successful,
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    94
     * and this task has not started when {@code cancel} is called,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * this task should never run.  If the task has already started,
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
    96
     * then the {@code mayInterruptIfRunning} parameter determines
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * whether the thread executing this task should be interrupted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * an attempt to stop the task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * <p>After this method returns, subsequent calls to {@link #isDone} will
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   101
     * always return {@code true}.  Subsequent calls to {@link #isCancelled}
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   102
     * will always return {@code true} if this method returned {@code true}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   104
     * @param mayInterruptIfRunning {@code true} if the thread executing this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * task should be interrupted; otherwise, in-progress tasks are allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * to complete
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   107
     * @return {@code false} if the task could not be cancelled,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * typically because it has already completed normally;
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   109
     * {@code true} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    boolean cancel(boolean mayInterruptIfRunning);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   114
     * Returns {@code true} if this task was cancelled before it completed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   117
     * @return {@code true} if this task was cancelled before it completed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    boolean isCancelled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   122
     * Returns {@code true} if this task completed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Completion may be due to normal termination, an exception, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * cancellation -- in all of these cases, this method will return
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   126
     * {@code true}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
18790
d25399d849bc 8019370: Sync j.u.c Fork/Join from 166 to tl
psandoz
parents: 14325
diff changeset
   128
     * @return {@code true} if this task completed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    boolean isDone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * Waits if necessary for the computation to complete, and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * retrieves its result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @return the computed result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws CancellationException if the computation was cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @throws ExecutionException if the computation threw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws InterruptedException if the current thread was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    V get() throws InterruptedException, ExecutionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Waits if necessary for at most the given time for the computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * to complete, and then retrieves its result, if available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param timeout the maximum time to wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param unit the time unit of the timeout argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return the computed result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @throws CancellationException if the computation was cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws ExecutionException if the computation threw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws InterruptedException if the current thread was interrupted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * while waiting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws TimeoutException if the wait timed out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    V get(long timeout, TimeUnit unit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        throws InterruptedException, ExecutionException, TimeoutException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}