ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.24
Committed: Tue Apr 26 01:17:18 2005 UTC (19 years, 1 month ago) by jsr166
Branch: MAIN
Changes since 1.23: +4 -7 lines
Log Message:
doc fixes

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    
9     /**
10 dl 1.12 * A <tt>Future</tt> 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     * <tt>get</tt> when the computation has completed, blocking if
15 dl 1.17 * necessary until it is ready. Cancellation is performed by the
16     * <tt>cancel</tt> 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 dl 1.21 * If you would like to use a <tt>Future</tt> for the sake
20     * of cancellability but not provide a usable result, you can
21     * declare types of the form <tt>Future&lt;?&gt;</tt> and
22     * return <tt>null</tt> as a result of the underlying task.
23 tim 1.1 *
24     * <p>
25 dl 1.5 * <b>Sample Usage</b> (Note that the following classes are all
26     * made-up.) <p>
27 tim 1.1 * <pre>
28 dl 1.10 * interface ArchiveSearcher { String search(String target); }
29 tim 1.1 * class App {
30 tim 1.18 * ExecutorService executor = ...
31 dl 1.5 * ArchiveSearcher searcher = ...
32     * void showSearch(final String target) throws InterruptedException {
33 tim 1.18 * Future&lt;String&gt; future = executor.submit(new Callable&lt;String&gt;() {
34     * public String call() { return searcher.search(target); }
35     * });
36 dl 1.5 * displayOtherThings(); // do other things while searching
37 tim 1.1 * try {
38 dl 1.5 * displayText(future.get()); // use future
39 tim 1.8 * } catch (ExecutionException ex) { cleanup(); return; }
40 tim 1.1 * }
41     * }
42     * </pre>
43     *
44 jsr166 1.24 * The {@link FutureTask} class is an implementation of <tt>Future</tt> that
45     * implements <tt>Runnable</tt>, and so may be executed by an <tt>Executor</tt>.
46 tim 1.18 * For example, the above construction with <tt>submit</tt> could be replaced by:
47 dl 1.10 * <pre>
48 tim 1.18 * FutureTask&lt;String&gt; future =
49     * new FutureTask&lt;String&gt;(new Callable&lt;String&gt;() {
50     * public String call() {
51     * return searcher.search(target);
52     * }});
53     * executor.execute(future);
54 dl 1.10 * </pre>
55 tim 1.1 * @see FutureTask
56     * @see Executor
57 dl 1.12 * @since 1.5
58 dl 1.4 * @author Doug Lea
59 dl 1.16 * @param <V> The result type returned by this Future's <tt>get</tt> method
60 tim 1.1 */
61 dl 1.17 public interface Future<V> {
62    
63     /**
64     * Attempts to cancel execution of this task. This attempt will
65     * fail if the task has already completed, already been cancelled,
66     * or could not be cancelled for some other reason. If successful,
67     * and this task has not started when <tt>cancel</tt> is called,
68     * this task should never run. If the task has already started,
69 dl 1.23 * then the <tt>mayInterruptIfRunning</tt> parameter determines
70 dl 1.17 * whether the thread executing this task should be interrupted in
71     * an attempt to stop the task.
72     *
73     * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
74     * task should be interrupted; otherwise, in-progress tasks are allowed
75     * to complete
76     * @return <tt>false</tt> if the task could not be cancelled,
77 jozart 1.19 * typically because it has already completed normally;
78 dl 1.17 * <tt>true</tt> otherwise
79     */
80     boolean cancel(boolean mayInterruptIfRunning);
81    
82     /**
83     * Returns <tt>true</tt> if this task was cancelled before it completed
84     * normally.
85     *
86     * @return <tt>true</tt> if task was cancelled before it completed
87     */
88     boolean isCancelled();
89    
90     /**
91 jsr166 1.24 * Returns <tt>true</tt> if this task completed.
92 dl 1.17 *
93     * Completion may be due to normal termination, an exception, or
94     * cancellation -- in all of these cases, this method will return
95     * <tt>true</tt>.
96 jsr166 1.24 *
97 dl 1.17 * @return <tt>true</tt> if this task completed.
98     */
99     boolean isDone();
100 tim 1.1
101     /**
102 dl 1.23 * Waits if necessary for the computation to complete, and then
103 tim 1.1 * retrieves its result.
104     *
105 dl 1.23 * @return the computed result
106     * @throws CancellationException if the computation was cancelled
107     * @throws ExecutionException if the computation threw an
108 dl 1.4 * exception
109 dl 1.23 * @throws InterruptedException if the current thread was interrupted
110 dl 1.4 * while waiting
111 tim 1.1 */
112     V get() throws InterruptedException, ExecutionException;
113    
114     /**
115     * Waits if necessary for at most the given time for the computation
116 dl 1.23 * to complete, and then retrieves its result, if available.
117 tim 1.1 *
118     * @param timeout the maximum time to wait
119 dl 1.14 * @param unit the time unit of the timeout argument
120 dl 1.23 * @return the computed result
121     * @throws CancellationException if the computation was cancelled
122     * @throws ExecutionException if the computation threw an
123 dl 1.4 * exception
124 dl 1.23 * @throws InterruptedException if the current thread was interrupted
125 dl 1.4 * while waiting
126 tim 1.1 * @throws TimeoutException if the wait timed out
127     */
128 dl 1.14 V get(long timeout, TimeUnit unit)
129 tim 1.1 throws InterruptedException, ExecutionException, TimeoutException;
130     }