ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/Future.java
Revision: 1.15
Committed: Sat Oct 18 12:29:33 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE
Changes since 1.14: +1 -0 lines
Log Message:
Added docs for type params

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. Once the computation has completed,
16 * the computation cannot be cancelled.
17 *
18 * <p>
19 * <b>Sample Usage</b> (Note that the following classes are all
20 * made-up.) <p>
21 * <pre>
22 * interface ArchiveSearcher { String search(String target); }
23 * class App {
24 * Executor executor = ...
25 * ArchiveSearcher searcher = ...
26 * void showSearch(final String target) throws InterruptedException {
27 * Future&lt;String&gt; future =
28 * new FutureTask&lt;String&gt;(new Callable&lt;String&gt;() {
29 * public String call() {
30 * return searcher.search(target);
31 * }});
32 * executor.execute(future);
33 * displayOtherThings(); // do other things while searching
34 * try {
35 * displayText(future.get()); // use future
36 * } catch (ExecutionException ex) { cleanup(); return; }
37 * }
38 * }
39 * </pre>
40 *
41 * The {@link Executors} class contains more convenient methods
42 * for common usages. For example, the above explicit
43 * construction could be replaced with:
44 * <pre>
45 * Future&lt;String&gt; future = Executors.execute(executor,
46 * new Callable&lt;String&gt;() {
47 * public String call() {
48 * return searcher.search(target);
49 * }});
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
56 */
57 public interface Future<V> extends Cancellable {
58
59 /**
60 * Waits if necessary for computation to complete, and then
61 * retrieves its result.
62 *
63 * @return computed result
64 * @throws CancellationException if this future was cancelled.
65 * @throws ExecutionException if underlying computation threw an
66 * exception
67 * @throws InterruptedException if current thread was interrupted
68 * while waiting
69 */
70 V get() throws InterruptedException, ExecutionException;
71
72 /**
73 * Waits if necessary for at most the given time for the computation
74 * to complete, and then retrieves its result.
75 *
76 * @param timeout the maximum time to wait
77 * @param unit the time unit of the timeout argument
78 * @return computed result
79 * @throws ExecutionException if underlying computation threw an
80 * exception
81 * @throws InterruptedException if current thread was interrupted
82 * while waiting
83 * @throws TimeoutException if the wait timed out
84 */
85 V get(long timeout, TimeUnit unit)
86 throws InterruptedException, ExecutionException, TimeoutException;
87 }
88
89
90