ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.26
Committed: Sat Aug 27 23:23:18 2005 UTC (18 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +5 -3 lines
Log Message:
whitespace

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