ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.27
Committed: Sun Aug 28 00:04:39 2005 UTC (18 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.26: +7 -2 lines
Log Message:
Clarify Future.cancel

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 * @see FutureTask
60 * @see Executor
61 * @since 1.5
62 * @author Doug Lea
63 * @param <V> The result type returned by this Future's <tt>get</tt> method
64 */
65 public interface Future<V> {
66
67 /**
68 * Attempts to cancel execution of this task. This attempt will
69 * fail if the task has already completed, already been cancelled,
70 * or could not be cancelled for some other reason. If successful,
71 * and this task has not started when <tt>cancel</tt> is called,
72 * this task should never run. If the task has already started,
73 * then the <tt>mayInterruptIfRunning</tt> parameter determines
74 * whether the thread executing this task should be interrupted in
75 * an attempt to stop the task.
76 *
77 * <p>After this method returns, subsequent calls to {@link #isDone} will
78 * always return <tt>true</tt>. Subsequent calls to {@link #isCancelled}
79 * will always return <tt>true</tt> if this method returned <tt>true</tt>.
80 *
81 * @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
82 * task should be interrupted; otherwise, in-progress tasks are allowed
83 * to complete
84 * @return <tt>false</tt> if the task could not be cancelled,
85 * typically because it has already completed normally;
86 * <tt>true</tt> otherwise
87 */
88 boolean cancel(boolean mayInterruptIfRunning);
89
90 /**
91 * Returns <tt>true</tt> if this task was cancelled before it completed
92 * normally.
93 *
94 * @return <tt>true</tt> if task was cancelled before it completed
95 */
96 boolean isCancelled();
97
98 /**
99 * Returns <tt>true</tt> if this task completed.
100 *
101 * Completion may be due to normal termination, an exception, or
102 * cancellation -- in all of these cases, this method will return
103 * <tt>true</tt>.
104 *
105 * @return <tt>true</tt> if this task completed
106 */
107 boolean isDone();
108
109 /**
110 * Waits if necessary for the computation to complete, and then
111 * retrieves its result.
112 *
113 * @return the computed result
114 * @throws CancellationException if the computation was cancelled
115 * @throws ExecutionException if the computation threw an
116 * exception
117 * @throws InterruptedException if the current thread was interrupted
118 * while waiting
119 */
120 V get() throws InterruptedException, ExecutionException;
121
122 /**
123 * Waits if necessary for at most the given time for the computation
124 * to complete, and then retrieves its result, if available.
125 *
126 * @param timeout the maximum time to wait
127 * @param unit the time unit of the timeout argument
128 * @return the computed result
129 * @throws CancellationException if the computation was cancelled
130 * @throws ExecutionException if the computation threw an
131 * exception
132 * @throws InterruptedException if the current thread was interrupted
133 * while waiting
134 * @throws TimeoutException if the wait timed out
135 */
136 V get(long timeout, TimeUnit unit)
137 throws InterruptedException, ExecutionException, TimeoutException;
138 }