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

# 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() { return searcher.search(target); }
38 * });
39 * displayOtherThings(); // do other things while searching
40 * try {
41 * displayText(future.get()); // use future
42 * } catch (ExecutionException ex) { cleanup(); return; }
43 * }
44 * }
45 * </pre>
46 *
47 * 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 * For example, the above construction with <tt>submit</tt> could be replaced by:
50 * <pre>
51 * 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 * </pre>
58 * @see FutureTask
59 * @see Executor
60 * @since 1.5
61 * @author Doug Lea
62 * @param <V> The result type returned by this Future's <tt>get</tt> method
63 */
64 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 * then the <tt>mayInterruptIfRunning</tt> parameter determines
73 * 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 * typically because it has already completed normally;
81 * <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 * Returns <tt>true</tt> if this task completed.
95 *
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 *
100 * @return <tt>true</tt> if this task completed
101 */
102 boolean isDone();
103
104 /**
105 * Waits if necessary for the computation to complete, and then
106 * retrieves its result.
107 *
108 * @return the computed result
109 * @throws CancellationException if the computation was cancelled
110 * @throws ExecutionException if the computation threw an
111 * exception
112 * @throws InterruptedException if the current thread was interrupted
113 * while waiting
114 */
115 V get() throws InterruptedException, ExecutionException;
116
117 /**
118 * Waits if necessary for at most the given time for the computation
119 * to complete, and then retrieves its result, if available.
120 *
121 * @param timeout the maximum time to wait
122 * @param unit the time unit of the timeout argument
123 * @return the computed result
124 * @throws CancellationException if the computation was cancelled
125 * @throws ExecutionException if the computation threw an
126 * exception
127 * @throws InterruptedException if the current thread was interrupted
128 * while waiting
129 * @throws TimeoutException if the wait timed out
130 */
131 V get(long timeout, TimeUnit unit)
132 throws InterruptedException, ExecutionException, TimeoutException;
133 }