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 restarted or 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<String> future = |
28 |
* new FutureTask<String>(new Callable<String>() { |
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<String> future = Executors.execute(executor, |
46 |
* new Callable<String>() { |
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 |
*/ |
56 |
public interface Future<V> extends Cancellable { |
57 |
|
58 |
/** |
59 |
* Waits if necessary for computation to complete, and then |
60 |
* retrieves its result. |
61 |
* |
62 |
* @return computed result |
63 |
* @throws CancellationException if this future was cancelled. |
64 |
* @throws ExecutionException if underlying computation threw an |
65 |
* exception |
66 |
* @throws InterruptedException if current thread was interrupted |
67 |
* while waiting |
68 |
*/ |
69 |
V get() throws InterruptedException, ExecutionException; |
70 |
|
71 |
/** |
72 |
* Waits if necessary for at most the given time for the computation |
73 |
* to complete, and then retrieves its result. |
74 |
* |
75 |
* @param timeout the maximum time to wait |
76 |
* @param granularity the time unit of the timeout argument |
77 |
* @return computed result |
78 |
* @throws ExecutionException if underlying computation threw an |
79 |
* exception |
80 |
* @throws InterruptedException if current thread was interrupted |
81 |
* while waiting |
82 |
* @throws TimeoutException if the wait timed out |
83 |
*/ |
84 |
V get(long timeout, TimeUnit granularity) |
85 |
throws InterruptedException, ExecutionException, TimeoutException; |
86 |
} |
87 |
|
88 |
|
89 |
|