• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Written by Doug Lea with assistance from members of JCP JSR-166
3  * Expert Group and released to the public domain, as explained at
4  * http://creativecommons.org/publicdomain/zero/1.0/
5  */
6 
7 package java.util.concurrent;
8 
9 /**
10  * A {@code Future} represents the result of an asynchronous
11  * computation.  Methods are provided to check if the computation is
12  * complete, to wait for its completion, and to retrieve the result of
13  * the computation.  The result can only be retrieved using method
14  * {@code get} when the computation has completed, blocking if
15  * necessary until it is ready.  Cancellation is performed by the
16  * {@code cancel} method.  Additional methods are provided to
17  * determine if the task completed normally or was cancelled. Once a
18  * computation has completed, the computation cannot be cancelled.
19  * If you would like to use a {@code Future} for the sake
20  * of cancellability but not provide a usable result, you can
21  * declare types of the form {@code Future<?>} and
22  * return {@code null} as a result of the underlying task.
23  *
24  * <p>
25  * <b>Sample Usage</b> (Note that the following classes are all
26  * made-up.)
27  *
28  * <pre> {@code
29  * interface ArchiveSearcher { String search(String target); }
30  * class App {
31  *   ExecutorService executor = ...
32  *   ArchiveSearcher searcher = ...
33  *   void showSearch(final String target)
34  *       throws InterruptedException {
35  *     Future<String> future
36  *       = executor.submit(new Callable<String>() {
37  *         public String call() {
38  *             return searcher.search(target);
39  *         }});
40  *     displayOtherThings(); // do other things while searching
41  *     try {
42  *       displayText(future.get()); // use future
43  *     } catch (ExecutionException ex) { cleanup(); return; }
44  *   }
45  * }}</pre>
46  *
47  * The {@link FutureTask} class is an implementation of {@code Future} that
48  * implements {@code Runnable}, and so may be executed by an {@code Executor}.
49  * For example, the above construction with {@code submit} could be replaced by:
50  * <pre> {@code
51  * FutureTask<String> future =
52  *   new FutureTask<>(new Callable<String>() {
53  *     public String call() {
54  *       return searcher.search(target);
55  *   }});
56  * executor.execute(future);}</pre>
57  *
58  * <p>Memory consistency effects: Actions taken by the asynchronous computation
59  * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
60  * actions following the corresponding {@code Future.get()} in another thread.
61  *
62  * @see FutureTask
63  * @see Executor
64  * @since 1.5
65  * @author Doug Lea
66  * @param <V> The result type returned by this Future's {@code get} method
67  */
68 public interface Future<V> {
69 
70     /**
71      * Attempts to cancel execution of this task.  This attempt will
72      * fail if the task has already completed, has already been cancelled,
73      * or could not be cancelled for some other reason. If successful,
74      * and this task has not started when {@code cancel} is called,
75      * this task should never run.  If the task has already started,
76      * then the {@code mayInterruptIfRunning} parameter determines
77      * whether the thread executing this task should be interrupted in
78      * an attempt to stop the task.
79      *
80      * <p>After this method returns, subsequent calls to {@link #isDone} will
81      * always return {@code true}.  Subsequent calls to {@link #isCancelled}
82      * will always return {@code true} if this method returned {@code true}.
83      *
84      * @param mayInterruptIfRunning {@code true} if the thread executing this
85      * task should be interrupted; otherwise, in-progress tasks are allowed
86      * to complete
87      * @return {@code false} if the task could not be cancelled,
88      * typically because it has already completed normally;
89      * {@code true} otherwise
90      */
cancel(boolean mayInterruptIfRunning)91     boolean cancel(boolean mayInterruptIfRunning);
92 
93     /**
94      * Returns {@code true} if this task was cancelled before it completed
95      * normally.
96      *
97      * @return {@code true} if this task was cancelled before it completed
98      */
isCancelled()99     boolean isCancelled();
100 
101     /**
102      * Returns {@code true} if this task completed.
103      *
104      * Completion may be due to normal termination, an exception, or
105      * cancellation -- in all of these cases, this method will return
106      * {@code true}.
107      *
108      * @return {@code true} if this task completed
109      */
isDone()110     boolean isDone();
111 
112     /**
113      * Waits if necessary for the computation to complete, and then
114      * retrieves its result.
115      *
116      * @return the computed result
117      * @throws CancellationException if the computation was cancelled
118      * @throws ExecutionException if the computation threw an
119      * exception
120      * @throws InterruptedException if the current thread was interrupted
121      * while waiting
122      */
get()123     V get() throws InterruptedException, ExecutionException;
124 
125     /**
126      * Waits if necessary for at most the given time for the computation
127      * to complete, and then retrieves its result, if available.
128      *
129      * @param timeout the maximum time to wait
130      * @param unit the time unit of the timeout argument
131      * @return the computed result
132      * @throws CancellationException if the computation was cancelled
133      * @throws ExecutionException if the computation threw an
134      * exception
135      * @throws InterruptedException if the current thread was interrupted
136      * while waiting
137      * @throws TimeoutException if the wait timed out
138      */
get(long timeout, TimeUnit unit)139     V get(long timeout, TimeUnit unit)
140         throws InterruptedException, ExecutionException, TimeoutException;
141 }
142