ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.31
Committed: Wed Sep 14 21:45:12 2005 UTC (18 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.30: +3 -4 lines
Log Message:
happens-before

File Contents

# User Rev Content
1 tim 1.1 /*
2 dl 1.12 * Written by Doug Lea with assistance from members of JCP JSR-166
3 dl 1.22 * Expert Group and released to the public domain, as explained at
4     * http://creativecommons.org/licenses/publicdomain
5 tim 1.1 */
6    
7     package java.util.concurrent;
8 jsr166 1.25 import java.util.concurrent.*; // for javadoc (till 6280605 is fixed)
9 tim 1.1
10     /**
11 dl 1.12 * 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 dl 1.17 * 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 dl 1.21 * 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 tim 1.1 *
25     * <p>
26 dl 1.5 * <b>Sample Usage</b> (Note that the following classes are all
27     * made-up.) <p>
28 tim 1.1 * <pre>
29 dl 1.10 * interface ArchiveSearcher { String search(String target); }
30 tim 1.1 * class App {
31 tim 1.18 * ExecutorService executor = ...
32 dl 1.5 * ArchiveSearcher searcher = ...
33 jsr166 1.26 * void showSearch(final String target)
34     * throws InterruptedException {
35     * Future&lt;String&gt; future
36     * = executor.submit(new Callable&lt;String&gt;() {
37 jsr166 1.27 * public String call() {
38     * return searcher.search(target);
39     * }});
40 dl 1.5 * displayOtherThings(); // do other things while searching
41 tim 1.1 * try {
42 dl 1.5 * displayText(future.get()); // use future
43 tim 1.8 * } catch (ExecutionException ex) { cleanup(); return; }
44 tim 1.1 * }
45     * }
46     * </pre>
47     *
48 jsr166 1.24 * 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 tim 1.18 * For example, the above construction with <tt>submit</tt> could be replaced by:
51 dl 1.10 * <pre>
52 tim 1.18 * 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 dl 1.10 * </pre>
59 brian 1.29 *
60 jsr166 1.31 * <p>Memory consistency effects: Actions taken by the asynchronous computation
61     * <a href="package-summary.html#MemoryVisibility"> <i>happen-before</i></a>
62     * actions following the corresponding {@code Future.get()} in another thread.
63 dl 1.30 *
64 tim 1.1 * @see FutureTask
65     * @see Executor
66 dl 1.12 * @since 1.5
67 dl 1.4 * @author Doug Lea
68 dl 1.16 * @param <V> The result type returned by this Future's <tt>get</tt> method
69 tim 1.1 */
70 dl 1.17 public interface Future<V> {
71    
72     /**
73     * Attempts to cancel execution of this task. This attempt will
74     * fail if the task has already completed, already been cancelled,
75     * or could not be cancelled for some other reason. If successful,
76     * and this task has not started when <tt>cancel</tt> is called,
77     * this task should never run. If the task has already started,
78 dl 1.23 * then the <tt>mayInterruptIfRunning</tt> parameter determines
79 dl 1.17 * whether the thread executing this task should be interrupted in
80     * an attempt to stop the task.
81     *
82 jsr166 1.27 * <p>After this method returns, subsequent calls to {@link #isDone} will
83     * always return <tt>true</tt>. Subsequent calls to {@link #isCancelled}
84     * will always return <tt>true</tt> if this method returned <tt>true</tt>.
85     *
86 dl 1.17 * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
87     * task should be interrupted; otherwise, in-progress tasks are allowed
88     * to complete
89     * @return <tt>false</tt> if the task could not be cancelled,
90 jozart 1.19 * typically because it has already completed normally;
91 dl 1.17 * <tt>true</tt> otherwise
92     */
93     boolean cancel(boolean mayInterruptIfRunning);
94    
95     /**
96     * Returns <tt>true</tt> if this task was cancelled before it completed
97     * normally.
98     *
99 jsr166 1.28 * @return <tt>true</tt> if this task was cancelled before it completed
100 dl 1.17 */
101     boolean isCancelled();
102    
103     /**
104 jsr166 1.24 * Returns <tt>true</tt> if this task completed.
105 dl 1.17 *
106     * Completion may be due to normal termination, an exception, or
107     * cancellation -- in all of these cases, this method will return
108     * <tt>true</tt>.
109 jsr166 1.24 *
110 jsr166 1.26 * @return <tt>true</tt> if this task completed
111 dl 1.17 */
112     boolean isDone();
113 tim 1.1
114     /**
115 dl 1.23 * Waits if necessary for the computation to complete, and then
116 tim 1.1 * retrieves its result.
117     *
118 dl 1.23 * @return the computed result
119     * @throws CancellationException if the computation was cancelled
120     * @throws ExecutionException if the computation threw an
121 dl 1.4 * exception
122 dl 1.23 * @throws InterruptedException if the current thread was interrupted
123 dl 1.4 * while waiting
124 tim 1.1 */
125     V get() throws InterruptedException, ExecutionException;
126    
127     /**
128     * Waits if necessary for at most the given time for the computation
129 dl 1.23 * to complete, and then retrieves its result, if available.
130 tim 1.1 *
131     * @param timeout the maximum time to wait
132 dl 1.14 * @param unit the time unit of the timeout argument
133 dl 1.23 * @return the computed result
134     * @throws CancellationException if the computation was cancelled
135     * @throws ExecutionException if the computation threw an
136 dl 1.4 * exception
137 dl 1.23 * @throws InterruptedException if the current thread was interrupted
138 dl 1.4 * while waiting
139 tim 1.1 * @throws TimeoutException if the wait timed out
140     */
141 dl 1.14 V get(long timeout, TimeUnit unit)
142 tim 1.1 throws InterruptedException, ExecutionException, TimeoutException;
143     }