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 |
* <b>Sample Usage</b> <p> |
18 |
* <pre> |
19 |
* class Image { ... }; |
20 |
* class ImageRenderer { Image render(byte[] raw); } |
21 |
* class App { |
22 |
* Executor executor = ... |
23 |
* ImageRenderer renderer = ... |
24 |
* void display(final byte[] rawimage) throws InterruptedException { |
25 |
* Future futureImage = |
26 |
* new FutureTask(new Callable() { |
27 |
* public Object call() { |
28 |
* return renderer.render(rawImage); |
29 |
* }}); |
30 |
* executor.execute(futureImage); |
31 |
* drawBorders(); // do other things while executing |
32 |
* drawCaption(); |
33 |
* try { |
34 |
* drawImage((Image)(futureImage.get())); // use future |
35 |
* } |
36 |
* catch (ExecutionException ex) { cleanup(); return; } |
37 |
* } |
38 |
* } |
39 |
* </pre> |
40 |
* |
41 |
* @since 1.5 |
42 |
* @see FutureTask |
43 |
* @see Executor |
44 |
* |
45 |
* @spec JSR-166 |
46 |
* @revised $Date: 2003/05/29 13:21:28 $ |
47 |
* @editor $Author: dl $ |
48 |
* @author Doug Lea |
49 |
*/ |
50 |
public interface Future<V> { |
51 |
|
52 |
/** |
53 |
* Returns <tt>true</tt> if the underlying task has completed. |
54 |
* |
55 |
* @fixme relation to isDone in Cancellable? |
56 |
* |
57 |
* @return <tt>true</tt> if underlying task has completed |
58 |
*/ |
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 |
* @throws ExecutionException if underlying computation threw an |
68 |
* exception |
69 |
* @throws InterruptedException if current thread was interrupted |
70 |
* while waiting |
71 |
*/ |
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 |
* @throws ExecutionException if underlying computation threw an |
82 |
* exception |
83 |
* @throws InterruptedException if current thread was interrupted |
84 |
* while waiting |
85 |
* @throws TimeoutException if the wait timed out |
86 |
*/ |
87 |
V get(long timeout, TimeUnit granularity) |
88 |
throws InterruptedException, ExecutionException, TimeoutException; |
89 |
} |
90 |
|
91 |
|
92 |
|