ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.30
Committed: Thu Sep 8 00:04:00 2005 UTC (18 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.29: +5 -0 lines
Log Message:
Edit pass for happens-before descriptions

File Contents

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