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