1 |
tim |
1.1 |
/* |
2 |
|
|
* @(#)Future.java |
3 |
|
|
*/ |
4 |
|
|
|
5 |
|
|
package java.util.concurrent; |
6 |
|
|
|
7 |
|
|
/** |
8 |
|
|
* A <tt>Future</tt> represents the result of an asynchronous computation. |
9 |
|
|
* Methods are provided to check if the computation is complete, |
10 |
|
|
* to wait for its completion, and to retrieve the result of the |
11 |
|
|
* computation. The result can only be retrieved when the computation |
12 |
|
|
* has completed. The <tt>get</tt> method will block until the computation |
13 |
|
|
* has completed. Once the computation has completed, the result cannot |
14 |
|
|
* be changed, nor can the computation be restarted or cancelled. |
15 |
|
|
* |
16 |
|
|
* <p> |
17 |
dl |
1.5 |
* <b>Sample Usage</b> (Note that the following classes are all |
18 |
|
|
* made-up.) <p> |
19 |
tim |
1.1 |
* <pre> |
20 |
dl |
1.5 |
* class ArchiveSearcher { String search(String target); } |
21 |
tim |
1.1 |
* class App { |
22 |
|
|
* Executor executor = ... |
23 |
dl |
1.5 |
* ArchiveSearcher searcher = ... |
24 |
|
|
* void showSearch(final String target) throws InterruptedException { |
25 |
tim |
1.7 |
* Future<String> future = |
26 |
|
|
* new FutureTask<String>(new Callable<String>() { |
27 |
dl |
1.5 |
* public String call() { |
28 |
|
|
* return searcher.search(target); |
29 |
tim |
1.1 |
* }}); |
30 |
dl |
1.5 |
* executor.execute(future); |
31 |
|
|
* displayOtherThings(); // do other things while searching |
32 |
tim |
1.1 |
* try { |
33 |
dl |
1.5 |
* displayText(future.get()); // use future |
34 |
tim |
1.8 |
* } catch (ExecutionException ex) { cleanup(); return; } |
35 |
tim |
1.1 |
* } |
36 |
|
|
* } |
37 |
|
|
* </pre> |
38 |
|
|
* |
39 |
|
|
* @since 1.5 |
40 |
|
|
* @see FutureTask |
41 |
|
|
* @see Executor |
42 |
|
|
* |
43 |
|
|
* @spec JSR-166 |
44 |
tim |
1.8 |
* @revised $Date: 2003/08/06 18:22:09 $ |
45 |
|
|
* @editor $Author: tim $ |
46 |
dl |
1.4 |
* @author Doug Lea |
47 |
tim |
1.1 |
*/ |
48 |
|
|
public interface Future<V> { |
49 |
|
|
|
50 |
|
|
/** |
51 |
|
|
* Returns <tt>true</tt> if the underlying task has completed. |
52 |
|
|
* |
53 |
dl |
1.6 |
* Completion may be due to normal termination, an exception, or |
54 |
|
|
* cancellation -- in all of these cases, this method will return |
55 |
|
|
* <tt>true</tt>. |
56 |
tim |
1.1 |
* |
57 |
dl |
1.6 |
* @return <tt>true</tt> if the underlying task has completed |
58 |
tim |
1.1 |
*/ |
59 |
|
|
boolean isDone(); |
60 |
|
|
|
61 |
|
|
/** |
62 |
|
|
* Waits if necessary for computation to complete, and then |
63 |
|
|
* retrieves its result. |
64 |
|
|
* |
65 |
|
|
* @return computed result |
66 |
|
|
* @throws CancellationException here??? |
67 |
dl |
1.4 |
* @throws ExecutionException if underlying computation threw an |
68 |
|
|
* exception |
69 |
|
|
* @throws InterruptedException if current thread was interrupted |
70 |
|
|
* while waiting |
71 |
tim |
1.1 |
*/ |
72 |
|
|
V get() throws InterruptedException, ExecutionException; |
73 |
|
|
|
74 |
|
|
/** |
75 |
|
|
* Waits if necessary for at most the given time for the computation |
76 |
|
|
* to complete, and then retrieves its result. |
77 |
|
|
* |
78 |
|
|
* @param timeout the maximum time to wait |
79 |
|
|
* @param granularity the time unit of the timeout argument |
80 |
|
|
* @return computed result |
81 |
dl |
1.4 |
* @throws ExecutionException if underlying computation threw an |
82 |
|
|
* exception |
83 |
|
|
* @throws InterruptedException if current thread was interrupted |
84 |
|
|
* while waiting |
85 |
tim |
1.1 |
* @throws TimeoutException if the wait timed out |
86 |
|
|
*/ |
87 |
|
|
V get(long timeout, TimeUnit granularity) |
88 |
|
|
throws InterruptedException, ExecutionException, TimeoutException; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
|
92 |
|
|
|