ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.18
Committed: Wed Dec 10 17:01:58 2003 UTC (20 years, 5 months ago) by tim
Branch: MAIN
Changes since 1.17: +13 -15 lines
Log Message:
Another use of Executors.execute in sample code.

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